Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT JAVASCRIPT
13.2. Promise.all
Medium
Topics
This question is in
13. Promise from scratchA dashboard loads the user, their projects and their notifications at the same time, and shows the page once all three have arrived. Waiting for them one after another would add the three delays together; starting all three and waiting for the group takes only as long as the slowest. Promise.all is that "wait for the group": it turns many promises into one promise for an array of results, and it fails fast when any of them fails.
Write promiseAll(iterable) in promise-all.js, a function that behaves like Promise.all for every case below.
Signature
function promiseAll(iterable) // returns a Promise for an arrayiterable: anything you can loop over withfor...of: an array, aSet, a string, a generator. Its items can be promises, thenables (objects with athenmethod) or plain values.- Returns a
Promisethat fulfills with a new array of the values, or rejects with the first reason.
Requirements
promiseAllreturns aPromise, and it does not throw: every problem, even a bad argument, becomes a rejection.- The array has the values in the order of the input, whatever order they settle in.
- It fulfills only when every item has fulfilled: never early, even when the last item settles first.
- A plain value counts as already fulfilled, and a thenable is followed, exactly as
Promise.resolvewould. - An empty iterable fulfills with
[]. - Any iterable works, not only arrays. The whole iterable is read during the call.
- It rejects as soon as one item rejects, with that item's reason: the first rejection in time wins, and nothing after it changes the result.
- A value that is not iterable (
5,undefined, a plain object) rejects with aTypeError. An error thrown while iterating rejects with that error. - Holes in an array count as
undefined; the same promise listed twice gives its value twice. - The result is a new array; the input is not changed.
- It does not call
Promise.all.
Examples
const slow = new Promise((resolve) => setTimeout(resolve, 100, "slow"));
promiseAll([slow, 2, Promise.resolve(3)]).then(console.log);
// after 100 ms: ["slow", 2, 3]: input order, not finishing orderpromiseAll([]).then(console.log); // []
promiseAll(new Set(["a", "b"])).then(console.log); // ["a", "b"]promiseAll([Promise.reject(new Error("no")), new Promise(() => {})])
.catch((error) => console.log(error.message)); // "no", without waiting for the secondpromiseAll(5).catch((error) => console.log(error instanceof TypeError)); // trueEnvironment
- Plain JavaScript in a browser page; the tests call the global
promiseAll. - You may use
new PromiseandPromise.resolve. You may not callPromise.allitself: one test replaces it for a moment to check. this-based calls (promiseAll.call(SomeClass, items)) are out of scope.
Hints
- If item 2 fulfills first, how do you know where its value goes, and how do you know when the array is complete?
- What does a
for...ofloop do with a number, and where would that error go if the loop ran inside the executor ofnew Promise?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…