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