intermediate
Promises and async/await
Promise states, chaining, combinators, async function return values, and await scheduling.
A Promise represents eventual fulfillment or rejection. `then` returns a new Promise. `async` functions always return Promises; `await` pauses only the async function, not the caller's thread.
`Promise.all` is fail-fast. `Promise.allSettled` waits for all. `Promise.race` resolves or rejects with the first settled. `Promise.any` resolves with the first fulfillment.
Work often starts when the executor or async function body runs — not when you `await`.
Promise.all([Promise.resolve('ok'), Promise.reject(new Error('bad'))])
.catch((e) => console.log(e.message)); // bad
On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.
Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.
The trade-off is often clarity versus performance or safety versus convenience.
Checklist:
- Separate creation from awaiting.
- Choose the right combinator.
- Propagate rejections explicitly.