Study notes · 15% of the exam
Promises and async/await
Promises represent future values. Know the combinators and how to run work in parallel.
Key points
- 1
An
asyncfunction always returns a promise;awaitunwraps it or throws on rejection, sotry/catchworks. - 2
Promise.allfails fast;allSettledreports every outcome;racetakes the first to settle;anytakes the first success. - 3
Work runs in parallel when every promise is created before you wait on it.
awaitinside a loop runs the steps one by one. - 4
Unhandled rejections are reported by the runtime. Always return or await the promises you create.
Common traps
forEachdoes not wait for async callbacks; usefor...ofwithawait, orPromise.all(array.map(...)).
Read the source
Test yourself on Promises and async/await
Ten questions, with the answer and explanation after each one.