Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT JAVASCRIPT
13.3. Promise.allSettled
Medium
Topics
This question is in
13. Promise from scratchA settings page saves five independent preferences at once. If one save fails, the other four still count, and the page should tell the user exactly which one failed. Promise.all is the wrong tool here: it gives up at the first failure and hides the rest. Promise.allSettled waits for every promise, whatever happens, and reports each outcome.
Write promiseAllSettled(iterable) in promise-all-settled.js, a function that behaves like Promise.allSettled for every case below.
Signature
function promiseAllSettled(iterable) // returns a Promise for an array of recordsiterable: anythingfor...ofcan loop over; its items can be promises, thenables or plain values.- The result fulfills with one record per item, in the order of the input:
{ status: "fulfilled", value }for an item that fulfilled,{ status: "rejected", reason }for an item that rejected.
Requirements
promiseAllSettledreturns aPromise, and it does not throw.- It fulfills only when every item has settled, fulfilled or rejected; never early.
- Each record has exactly two properties:
statusandvalue, orstatusandreason. Nothing else. - Records are in the order of the input, not the order the items settled in.
- A rejection never rejects the result: it becomes a record. That includes reasons like
undefined,0ornull. - Plain values become fulfilled records; thenables are followed, exactly as
Promise.resolvewould. - An empty iterable fulfills with
[]. Any iterable works (arrays,Set, strings, generators), and it is read during the call. - The only way the result rejects: a value that is not iterable (
TypeError), or an error thrown while iterating (that error). - The result is a new array of new record objects.
- It does not call
Promise.allSettled.
Examples
promiseAllSettled([Promise.resolve(1), Promise.reject(new Error("no")), "x"])
.then(console.log);
// [
// { status: "fulfilled", value: 1 },
// { status: "rejected", reason: Error: no },
// { status: "fulfilled", value: "x" },
// ]promiseAllSettled([]).then(console.log); // []promiseAllSettled([Promise.reject(undefined)]).then(console.log);
// [{ status: "rejected", reason: undefined }]promiseAllSettled(null).catch((error) => console.log(error instanceof TypeError)); // trueEnvironment
- Plain JavaScript in a browser page; the tests call the global
promiseAllSettled. - You may use
new Promise,Promise.resolve, and other combinators. You may not callPromise.allSettleditself: one test replaces it for a moment to check. this-based calls are out of scope.
Hints
- How can a
thenwith two handlers turn both outcomes into the same kind of thing? - When the last item settles first, how do you know the array is not complete yet?
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…