1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var call = require('../internals/function-call');
|
---|
4 | var aCallable = require('../internals/a-callable');
|
---|
5 | var newPromiseCapabilityModule = require('../internals/new-promise-capability');
|
---|
6 | var perform = require('../internals/perform');
|
---|
7 | var iterate = require('../internals/iterate');
|
---|
8 | var PROMISE_STATICS_INCORRECT_ITERATION = require('../internals/promise-statics-incorrect-iteration');
|
---|
9 |
|
---|
10 | // `Promise.all` method
|
---|
11 | // https://tc39.es/ecma262/#sec-promise.all
|
---|
12 | $({ target: 'Promise', stat: true, forced: PROMISE_STATICS_INCORRECT_ITERATION }, {
|
---|
13 | all: function all(iterable) {
|
---|
14 | var C = this;
|
---|
15 | var capability = newPromiseCapabilityModule.f(C);
|
---|
16 | var resolve = capability.resolve;
|
---|
17 | var reject = capability.reject;
|
---|
18 | var result = perform(function () {
|
---|
19 | var $promiseResolve = aCallable(C.resolve);
|
---|
20 | var values = [];
|
---|
21 | var counter = 0;
|
---|
22 | var remaining = 1;
|
---|
23 | iterate(iterable, function (promise) {
|
---|
24 | var index = counter++;
|
---|
25 | var alreadyCalled = false;
|
---|
26 | remaining++;
|
---|
27 | call($promiseResolve, C, promise).then(function (value) {
|
---|
28 | if (alreadyCalled) return;
|
---|
29 | alreadyCalled = true;
|
---|
30 | values[index] = value;
|
---|
31 | --remaining || resolve(values);
|
---|
32 | }, reject);
|
---|
33 | });
|
---|
34 | --remaining || resolve(values);
|
---|
35 | });
|
---|
36 | if (result.error) reject(result.value);
|
---|
37 | return capability.promise;
|
---|
38 | }
|
---|
39 | });
|
---|