← Lessons

quiz vs the machine

Gold1400

Frontend

Promises and Async Patterns

Promises model a future value, and async await makes chains read like ordinary code.

5 min read · core · beat Gold to climb

A value that is not ready yet

A promise represents a result that will arrive later. It sits in one of three states: pending, fulfilled, or rejected. Once settled it never changes, and its then callbacks run as microtasks.

  • then receives the fulfilled value and returns a new promise.
  • catch handles a rejection anywhere earlier in the chain.
  • finally runs regardless of outcome.

Async await and combinators

The async await syntax pauses a function until a promise settles, letting you write sequential looking code while staying non blocking. An async function always returns a promise.

  • Promise all: waits for every promise, rejecting on the first failure.
  • Promise allSettled: waits for all and reports each result.
  • Promise race: settles with the first promise to finish.

A frequent mistake is awaiting in a loop when the calls are independent, which serializes work that could run in parallel. Group them with Promise all instead. Always handle rejection, since an unhandled rejection can crash or warn.

Key idea

Promises settle once and run as microtasks, while async await and combinators like Promise all control whether work runs in sequence or in parallel.

Check yourself

Answer to earn rating on the learn ladder.

1. What does Promise all do when one input rejects?

2. What does an async function always return?

3. Which combinator reports the outcome of every promise?