JavaScript, inside out · Chapter 49 of 51

Promise combinators

By the end of this chapter you can write Promise.all, allSettled, race and any yourself, with new Promise and then. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 4 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Round four. Dev leans back. "The order screen loads the menu, the customer's loyalty points and the rider's ETA: three requests. How do you wait for all of them?" Promise.all, obviously. "Good. Now write it. Then write the other three."

In chapters 40 and 42 you used Promise.all and allSettled. Using them is not the same as knowing what they promise: in which order the results come back, what happens when one input fails, and what an empty list does. Each of the four is about fifteen lines, and each has one detail most candidates miss.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: promiseAll, your own Promise.all

    Write promiseAll(items). items is any iterable (an array, a Set) of promises or plain values. Return a new promise that fulfills with an array of every result in input order, not in the order they finished, once all of them have fulfilled.

    As soon as any input rejects, reject with that reason, without waiting for the rest; later outcomes change nothing, and with two rejections the earlier one in time wins. Plain values count as already fulfilled. Empty input fulfills with an empty array.

    Use new Promise, Promise.resolve and then: the check fails if you call the native Promise.all, allSettled, race or any.

  2. Task 2 · interview question

    Interview: promiseAllSettled, every outcome, never rejects

    Write promiseAllSettled(items). Return a new promise that waits for every input and never rejects. It fulfills with an array in input order: for each input, either an object with status 'fulfilled' and value, or an object with status 'rejected' and reason (exactly those two keys).

    Even when every input rejects, it fulfills. Plain values are fulfilled, and empty input fulfills with an empty array. Do not call the native Promise.all, allSettled, race or any.

  3. Task 3 · interview question

    Interview: promiseRace, the first to settle wins

    Write promiseRace(items). Return a new promise that settles the same way as the first input to settle: fulfilled with its value, or rejected with its reason. Later outcomes change nothing. A plain value counts as already settled.

    Empty input stays pending for ever. Do not call the native Promise.all, allSettled, race or any.

  4. Task 4 · interview question

    Interview: promiseAny, the first success, or an AggregateError

    Write promiseAny(items). Return a new promise that fulfills with the value of the first input to fulfill, skipping rejections on the way. A plain value counts as a success.

    Only when every input has rejected does it reject, with an AggregateError whose errors array holds the reasons in input order, not the order they failed in. Empty input rejects at once with an AggregateError whose errors is an empty array. Do not call the native Promise.all, allSettled, race or any.

What you will learn

  • A promise settles once: the first resolve or reject wins.
  • Plain values and iterables: Promise.resolve treats a value and a promise alike, and spread turns any iterable into an array.
  • Finishing order is not input order: why results go in by index, with a counter.
  • AggregateError: one error that carries a list of errors.

More in The interview

Build "Promise combinators" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.