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