13.5. Promise.any with AggregateError
Topics
This question is in
13. Promise from scratchAn app can read a user's profile from three places: a memory cache, the browser's storage, and the server. Any one of them is good enough, and a failure in one should not matter while another can still answer. Only when all of them fail is there an error to show, and then the user (or the log) needs every reason, not just one. Promise.any does exactly that: the first success wins, and if there is none, it rejects with an AggregateError that carries every failure.
Write promiseAny(iterable) in promise-any.js, a function that behaves like Promise.any for every case below.
Signature
function promiseAny(iterable) // returns a Promiseiterable: anythingfor...ofcan loop over; its items can be promises, thenables or plain values.- Returns a
Promisethat fulfills with the first value, or rejects with anAggregateError. AnAggregateErroris a built-in error class:new AggregateError(errors, message)makes one whoseerrorsproperty is an array of the reasons.
Requirements
promiseAnyreturns aPromise, and it does not throw.- It fulfills with the value of the first item to fulfill. Rejections before that are ignored.
- While at least one item is pending, a rejection never rejects the result.
- When every item has rejected, it rejects with an
AggregateError. Itserrorsis an array with every reason, in the order of the input, not the order they came in. Reasons likeundefinedor0are kept. - An empty iterable rejects with an
AggregateErrorwhoseerrorsis[]. - Nothing that settles after the winner changes the result.
- A plain value counts as already fulfilled; thenables are followed, exactly as
Promise.resolvewould. - Any iterable works (arrays,
Set, strings, generators), and it is read during the call. - A value that is not iterable rejects with a
TypeError(not anAggregateError); an error thrown while iterating rejects with that error. - It does not call
Promise.any.
Examples
promiseAny([Promise.reject(new Error("cache miss")), Promise.resolve("from server")])
.then(console.log); // "from server"const late = new Promise((_, reject) => setTimeout(reject, 100, "late"));
promiseAny([late, Promise.reject("early")]).catch((error) => {
console.log(error instanceof AggregateError); // true
console.log(error.errors); // ["late", "early"]: input order
});promiseAny([]).catch((error) => console.log(error.errors)); // []promiseAny(42).catch((error) => console.log(error instanceof TypeError)); // trueEnvironment
- Plain JavaScript in a browser page; the tests call the global
promiseAny. - You may use
new Promise,Promise.resolve, theAggregateErrorclass and the other combinators. You may not callPromise.anyitself: one test replaces it for a moment to check. - The exact text of the error's
messageis up to you.
Hints
Promise.allfails at the first rejection and succeeds when all fulfill. Which of those two doesanyflip?- When the last item in the input rejects first, where should its reason go?
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