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