The Promise.all(iterable)
method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.
Syntax
Promise.all(iterable);
Parameters
Description
The result is passed as an array of values from all the promises. If something passed in the iterable array is not a promise, it's converted to one by Promise.resolve
. If any of the passed in promises rejects, the all
Promise immediately rejects with the value of the promise that rejected, discarding all the other promises whether or not they have resolved. If an empty array is passed, then this method resolves immediately.
Examples
Using Promise.all
Promise.all
waits for all fulfillments (or the first rejection).
var p1 = Promise.resolve(3); var p2 = 1337; var p3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, "foo"); }); Promise.all([p1, p2, p3]).then(function(values) { console.log(values); // [3, 1337, "foo"] });
Promise.all
fail-fast behaviour
Promise.all
is rejected if one of the elements is rejected and Promise.all
fails fast: If you have four promises which resolve after a timeout, and one rejects immediately, then Promise.all
rejects immediately.
var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 1000, "one"); }); var p2 = new Promise(function(resolve, reject) { setTimeout(resolve, 2000, "two"); }); var p3 = new Promise(function(resolve, reject) { setTimeout(resolve, 3000, "three"); }); var p4 = new Promise(function(resolve, reject) { setTimeout(resolve, 4000, "four"); }); var p5 = new Promise(function(resolve, reject) { reject("reject"); }); Promise.all([p1, p2, p3, p4, p5]).then(function(value) { console.log(value); }, function(reason) { console.log(reason) }); //From console: //"reject"
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.all' in that specification. |
Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise.all' in that specification. |
Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 32 | 29.0 (29.0) | No support | 19 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|
Basic support | No support | 29.0 (29.0) | No support | No support | 8 | 32 |