You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
47 lines
1.7 KiB
'use strict'; |
|
var $ = require('../internals/export'); |
|
var aCallable = require('../internals/a-callable'); |
|
var getBuiltIn = require('../internals/get-built-in'); |
|
var call = require('../internals/function-call'); |
|
var newPromiseCapabilityModule = require('../internals/new-promise-capability'); |
|
var perform = require('../internals/perform'); |
|
var iterate = require('../internals/iterate'); |
|
|
|
var PROMISE_ANY_ERROR = 'No one promise resolved'; |
|
|
|
// `Promise.any` method |
|
// https://tc39.es/ecma262/#sec-promise.any |
|
$({ target: 'Promise', stat: true }, { |
|
any: function any(iterable) { |
|
var C = this; |
|
var AggregateError = getBuiltIn('AggregateError'); |
|
var capability = newPromiseCapabilityModule.f(C); |
|
var resolve = capability.resolve; |
|
var reject = capability.reject; |
|
var result = perform(function () { |
|
var promiseResolve = aCallable(C.resolve); |
|
var errors = []; |
|
var counter = 0; |
|
var remaining = 1; |
|
var alreadyResolved = false; |
|
iterate(iterable, function (promise) { |
|
var index = counter++; |
|
var alreadyRejected = false; |
|
remaining++; |
|
call(promiseResolve, C, promise).then(function (value) { |
|
if (alreadyRejected || alreadyResolved) return; |
|
alreadyResolved = true; |
|
resolve(value); |
|
}, function (error) { |
|
if (alreadyRejected || alreadyResolved) return; |
|
alreadyRejected = true; |
|
errors[index] = error; |
|
--remaining || reject(new AggregateError(errors, PROMISE_ANY_ERROR)); |
|
}); |
|
}); |
|
--remaining || reject(new AggregateError(errors, PROMISE_ANY_ERROR)); |
|
}); |
|
if (result.error) reject(result.value); |
|
return capability.promise; |
|
} |
|
});
|
|
|