Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
async await完全に理解した
Search
asuka
November 25, 2021
95
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
async await完全に理解した
2021年のスライド
asuka
November 25, 2021
More Decks by asuka
See All by asuka
GoとSIMDとWasmの今。
askua
3
560
2025年ふりかえり
askua
1
240
ライブラリを公開してメンテナンスした一年
askua
0
100
Wasmの気になる最新情報
askua
1
390
Wasmのエコシステムを使った ツール作成方法
askua
0
410
Pure Goで体験するWasmの未来
askua
1
1.1k
Wasmで社内ツールを作って配布しよう
askua
0
320
Wasm元年
askua
0
380
wstdなんだか良さそう
askua
0
140
Featured
See All Featured
My Coaching Mixtape
mlcsv
0
170
What's in a price? How to price your products and services
michaelherold
247
13k
Chasing Engaging Ingredients in Design
codingconduct
0
240
How to build a perfect <img>
jonoalderson
1
5.8k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
180
Agile that works and the tools we love
rasmusluckow
331
22k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
260
Evolving SEO for Evolving Search Engines
ryanjones
0
240
Side Projects
sachag
455
43k
Discover your Explorer Soul
emna__ayadi
2
1.2k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
340
Believing is Seeing
oripsolob
1
170
Transcript
async await完全に理解した asuka y Nov. 25 2021
対象 JavaScript完全に理解した人
アジェンダ 1. async awaitとは何か 2. JavaScriptにおけるasync await 3. ベストプラクティス
1. async awaitとは何か
async awaitとは何か async await → 非同期関数の構文 非同期関数 非同期関数は async キーワードで宣言され、その中で
await キーワードを使うことができます。 async および await キーワードを使用することで、プロミスベースの非同期の動作を、プロミスチェー ンを明示的に構成する必要なく、よりすっきりとした方法で書くことができます。 MDN Web Docs
async awaitとは何か async await → 非同期関数の構文 → 糖衣構文だとわかる 非同期関数 非同期関数は
async キーワードで宣言され、その中で await キーワードを使うことができます。 async および await キーワードを使用することで、 プロミスベースの非同期の動作を、プロミスチェー ンを明示的に構成する必要なく、よりすっきりとした方法で書くことができます。 MDN Web Docs
そもそも非同期処理とは何か function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(()
=> { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { console.log('calling'); resolveAfter2Seconds(); console.log('called'); } asyncCall(); ①→ ❷→ ③→ start end ① calling ③ called ❷ resolved 非同期
そもそも非同期処理とは何か function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(()
=> { console.log('resolved'); resolve(); }, 2000); }); } async function asyncCall() { console.log('calling'); await resolveAfter2Seconds(); console.log('called'); } asyncCall(); // 非同期関数 ①→ ❷→ ③→ start end ① calling ③ called 非同期 ❷ resolved
async awaitを利用しない場合 function resolveAfter2Seconds() { return new Promise(resolve => {
setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { return new Promise(resolve => { console.log('calling'); resolveAfter2Seconds().then(() => { console.log('called'); }); }); } asyncCall(); // 非同期関数
async awaitを利用しない場合 function resolveAfter2Seconds() { return new Promise(resolve => {
setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { return new Promise(resolve => { console.log('calling'); resolveAfter2Seconds().then(() => { console.log('called'); }); }); } asyncCall(); // 非同期関数 function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } async function asyncCall() { console.log('calling'); await resolveAfter2Seconds(); console.log('called'); } asyncCall(); // 非同期関数 async awaitによってシンプルな構文になる (糖衣構文)
Promiseとは何か function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(()
=> { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { return new Promise(resolve => { console.log('calling'); resolveAfter2Seconds().then(() => { console.log('called'); }); }); } asyncCall(); // 非同期関数 function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } async function asyncCall() { console.log('calling'); await resolveAfter2Seconds(); console.log('called'); } asyncCall(); // 非同期関数 async awaitによってシンプルな構文になる (糖衣構文)
Promiseとは何か Promise → 非同期処理の完了の結果およびその結果の値を表す C#のTask や DartのFuture に相当する Promise 作成された時点では分からなくてもよい値へのプロキシーです。
非同期のアクションの成功値または 失敗理由にハンドラーを結びつけることができます。 これにより、非同期メソッドは結果の値を返す 代わりに、未来のある時点で値を提供するプロミスを返すことで、同期メソッドと同じように値を返す ことができるようになります。 MDN Web Docs
Promiseとは何か ①→ ❷→ ③→ start end ① calling ③ called
❷ resolved 非同期 function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { return new Promise(resolve => { console.log('calling'); resolveAfter2Seconds().then(() => { console.log('called'); }); }); } asyncCall(); // 非同期関数 then(成功時)
Promiseとは何か ①→ ❷→ ③→ start end ① calling ③ called
❷ resolved 非同期 function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { console.log('resolved'); resolve(); }, 2000); }); } function asyncCall() { return new Promise(resolve => { console.log('calling'); resolveAfter2Seconds().then(() => { console.log('called'); }); }); } asyncCall(); // 非同期関数 then(成功時) 非同期のアクションの成功値または失敗理由にハンドラーを結び つけることができる
2. JavaScriptにおけるasync await
2. JavaScriptにおけるasync await 非同期処理
JavaScriptにおける非同期処理 GoやC#では非同期処理といえば並行処理であるが, 挙動から推測するにJavaScriptは並行処理ではない (おそらく歴史的経緯により) Dartもシングルスレッドである JavaScript プロトタイプベースで、 シングルスレッドで、動的型付けを持ち、そしてオブジェクト指向、命令型、宣 言型 (関数プログラミングなど
) といったスタイルをサポートするマルチパラダイムのスクリプト言語で す。 MDN Web Docs
スレッド2 並行処理 start end any... any... 非同期 メインスレッド any... ランタイム側で同時実行しているように
見せかけるようタスクをスケジュールす る
JavaScriptにおける非同期処理 start end any... any... 非同期 スレッド any... 1スレッド内で処理が行われるため,同時に複数の処理が 並行して行われることがない
JavaScriptにおける非同期処理 start end any... any... 非同期 スレッド while (true) {...}
1スレッド内で処理が行われるため,同時に複数の処理が 並行して行われることがない 非同期処理を呼び出した後,非同期処理が実行されるまで の処理が一向に終わらない場合,ブラウザはどうなるのか?
JavaScriptにおける非同期処理 start end any... any... 非同期 スレッド while (true) {...}
1スレッド内で処理が行われるため,同時に複数の処理が 並行して行われることがない 非同期処理を呼び出した後,非同期処理が実行されるまで の処理が一向に終わらない場合,ブラウザはどうなるのか? 画面が一切応答しなくなる (呼び出した非同期処理も実行されない )
JavaScriptにおける非同期処理 start end any... 非同期 スレッド while (true) {...} 1スレッド内で処理が行われるため,同時に複数の処理が
並行して行われることがない 非同期処理を呼び出した後,非同期処理が実行されるまで の処理が一向に終わらない場合,ブラウザはどうなるのか? 非同期処理が終わらないのもダメ any...
ベストプラクティス
ベストプラクティス 1. 1つの処理が数秒以上実行するような処理を書かない a. 小さい処理の実行単位で非同期処理にする 2. 非同期処理 ≠ 並行処理である a.
非同期で呼び出す処理自体も小さくする必要がある start end any... any... 非同期 スレッド any... 非同期 ...
async await完全に理解した END