Edge cases: empty arrays, plain values and non-iterables
Passing something that isn't iterable doesn't throw on the spot: the returned promise rejects with a TypeError. It prints before the plain-values line because the empty and broken inputs settle at once, while all needs a few microtask turns to collect three values.
Four static methods combine many promises into one: all waits for every one (and fails fast), allSettled waits for every outcome, race takes the first to settle, and any takes the first success.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Using wait(ms, value), load "A" (100 ms), "B" (200 ms) and "C" (50 ms) in parallel and log [ 'A', 'B', 'C' ].
Write firstAnswer(promises) that fulfils with the first success, or with the string "no answer" if every promise fails.
Explain it without notes
Compare Promise.all, allSettled, race and any: what does each fulfil and reject with?
If Promise.all makes nothing run in parallel, why does using it make code faster?
How would you add a timeout to a promise, and what is the limitation of doing it with race?
What is an AggregateError, and when do you see one?
Preview
Console
Nothing logged yet.Expected console output
all([]): []
allSettled([]): []
any([]): AggregateError
not an array: TypeError
plain values are fine: [ 1, 'two', 3 ]