If the browser is too old and does not support the latest Promise.allSettled API, we can use polyfill technology to simply use Promise.all and implement Promise.allSettled by ourselves.
The complete code is implemented as follows:
if (!Promise.allSettled) {
const rejectHandler = reason => ({ status: 'rejected', reason });
const resolveHandler = value => ({ status: 'fulfilled', value });
Promise.allSettled = function (promises) {
const convertedPromises = promises.map(p => Promise.resolve(p).then(resolveHandler, rejectHandler));
return Promise.all(convertedPromises);
};
}
In this code, promises.map takes input values, converts them to Promises using p => Promise.resolve(p)
(in case a non-Promise primitive value is passed), then adds a .then handler to each value .
This handler converts the successful result value to {status:'fulfilled', value}
and the error cause to {status:'rejected', reason}
. This is exactly the format of Promise.allSettled
.
Now we can use Promise.allSettled to get the results of all given Promises, even if some of them are rejected.
Promise.race
Similar to Promise.all, but only waits for the first resolved Promise and gets its result (or error).
grammar:
let promise = Promise.race(iterable);
The following code will print the value of the first Promise whose state becomes fulfilled, which is 1:
Promise.race([
new Promise((resolve, reject) => setTimeout(() => resolve(1), 1000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")), 2000)),
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
]).then(alert); // 1
The first promise here is the fastest, so it becomes the result. After the first confirmed Promise win the race
all further results/errors are ignored.
Promise.any
Similar to Promise.race, but only waits for the first fulfilled Promise and gets its result. If all of the given Promises are rejected, the returned Promise is rejected by AggregateError -- a special error object that stores all Promise errors in its errors property.
grammar:
let promise = Promise.any(iterable);
The following example results in 1:
Promise.any([
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Whoops!")), 1000)),
new Promise((resolve, reject) => setTimeout(() => resolve(1), 2000)),
new Promise((resolve, reject) => setTimeout(() => resolve(3), 3000))
]).then(alert); // 1
The first promise here was the fastest, but was rejected, so the second promise became the result. After the first fulfilled Promise win the race
all further results will be ignored.
Here's an example where all Promises fail:
Promise.any([
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Ouch!")), 1000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error("Error!")), 2000))
]).catch(error => {
console.log(error.constructor.name); // AggregateError
console.log(error.errors[0]); // Error: Ouch!
console.log(error.errors[1]); // Error: Error!
});
As we can see, the error object of a failed Promise is available in the errors property of the AggregateError object.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。