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. 1

    An async function always returns a promise; await unwraps it or throws on rejection, so try/catch works.

  2. 2

    Promise.all fails fast; allSettled reports every outcome; race takes the first to settle; any takes the first success.

  3. 3

    Work runs in parallel when every promise is created before you wait on it. await inside a loop runs the steps one by one.

  4. 4

    Unhandled rejections are reported by the runtime. Always return or await the promises you create.

Common traps

  • forEach does not wait for async callbacks; use for...of with await, or Promise.all(array.map(...)).

Test yourself on Promises and async/await

Ten questions, with the answer and explanation after each one.