This is the 18th day of my participation in the August Genwen Challenge.More challenges in August
Promise.all()
The promise.all () method accepts an iterable of promises. Array, Map, and Set are all ES6 iterable types), and only one Promise instance is returned. The result of the resolve callbacks to all of those entered promises is an Array. The Promise’s resolve callback is executed when all the incoming Promise’s resolve callbacks have ended, or when no promises are left in the input iterable. Its Reject callback execution throws an error as soon as any incoming promise’s Reject callback is executed or an invalid promise is entered, and reject is the first error message thrown.
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
// expected output: Array [3, 42, "foo"]
Copy the code
Promise.all(iterable);
parameter
-
可迭代
-
An iterable, such as [Array] or [String].
[Return value]
- If the argument passed is an empty iterable, return a Promise that is already resolved.
- If the parameter passed does not contain any
promise
, returns oneAsynchronously ResolvedPromise
. Note: Google Chrome 58 returns one in this caseAlready resolvedThe state of thePromise
. - Otherwise return onePending 的
Promise
. This returnspromise
And then there will be at allpromise
Both completed or have onepromise
When the failureasynchronousTo complete or fail. See below for an example of “asynchrony or synchronization for Promise.all”. The return value will be the same as that in the argumentpromise
Order, not by callpromise
To determine the order of completion.
[that]
This method is useful when gathering the return results of multiple promises.
Fulfillment: If the iterable passed in is empty, promise.all returns a Promise in a completed state synchronously. The promise returned by promise.all becomes completed asynchronously if all incoming promises become complete, or if there are no promises in the incoming iterable. In any case, the result of the completion state of the Promise returned by promise.all is an array containing the values of all the passed iteration parameter objects (including non-Promise values).
Rejection: If a promise is passed in with a failed promise, promise. all asynchronously gives that failed result to the failed state callback, regardless of whether the other promise is fulfilled or not.