| 1 |
import Enumerator from '../enumerator'; |
| 2 |
|
| 3 |
/** |
| 4 |
`Promise.all` accepts an array of promises, and returns a new promise which |
| 5 |
is fulfilled with an array of fulfillment values for the passed promises, or |
| 6 |
rejected with the reason of the first passed promise to be rejected. It casts all |
| 7 |
elements of the passed iterable to promises as it runs this algorithm. |
| 8 |
|
| 9 |
Example: |
| 10 |
|
| 11 |
```javascript |
| 12 |
let promise1 = resolve(1); |
| 13 |
let promise2 = resolve(2); |
| 14 |
let promise3 = resolve(3); |
| 15 |
let promises = [ promise1, promise2, promise3 ]; |
| 16 |
|
| 17 |
Promise.all(promises).then(function(array){ |
| 18 |
// The array here would be [ 1, 2, 3 ]; |
| 19 |
}); |
| 20 |
``` |
| 21 |
|
| 22 |
If any of the `promises` given to `all` are rejected, the first promise |
| 23 |
that is rejected will be given as an argument to the returned promises's |
| 24 |
rejection handler. For example: |
| 25 |
|
| 26 |
Example: |
| 27 |
|
| 28 |
```javascript |
| 29 |
let promise1 = resolve(1); |
| 30 |
let promise2 = reject(new Error("2")); |
| 31 |
let promise3 = reject(new Error("3")); |
| 32 |
let promises = [ promise1, promise2, promise3 ]; |
| 33 |
|
| 34 |
Promise.all(promises).then(function(array){ |
| 35 |
// Code here never runs because there are rejected promises! |
| 36 |
}, function(error) { |
| 37 |
// error.message === "2" |
| 38 |
}); |
| 39 |
``` |
| 40 |
|
| 41 |
@method all |
| 42 |
@static |
| 43 |
@param {Array} entries array of promises |
| 44 |
@param {String} label optional string for labeling the promise. |
| 45 |
Useful for tooling. |
| 46 |
@return {Promise} promise that is fulfilled when all `promises` have been |
| 47 |
fulfilled, or rejected if any of them become rejected. |
| 48 |
@static |
| 49 |
*/ |
| 50 |
export default function all(entries) { |
| 51 |
return new Enumerator(this, entries).promise; |
| 52 |
} |