1Promise.allSettled ( iterable )
The allSettled
function returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises have settled, i.e. become either fulfilled or rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
- Let C be the this value.
- If Type(C) is not Object, throw a TypeError exception.
- Let promiseCapability be ? NewPromiseCapability(C).
- Let iteratorRecord be GetIterator(iterable).
- IfAbruptRejectPromise(iteratorRecord, promiseCapability).
- Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
- If result is an abrupt completion, then
- If iteratorRecord.[[Done]] is false, set result to IteratorClose(iteratorRecord, result).
- IfAbruptRejectPromise(result, promiseCapability).
- Return Completion(result).
Note
The allSettled
function requires its this value to be a constructor function that supports the parameter conventions of the Promise
constructor.
1.1Runtime Semantics: PerformPromiseAllSettled ( iteratorRecord, constructor, resultCapability )
When the PerformPromiseAllSettled abstract operation is called with arguments iteratorRecord, constructor, and resultCapability, the following steps are taken:
- Assert: ! IsConstructor(constructor) is true.
- Assert: resultCapability is a PromiseCapability Record.
- Let values be a new empty List.
- Let remainingElementsCount be a new Record { [[Value]]: 1 }.
- Let index be 0.
- Let promiseResolve be ? Get(constructor,
"resolve"
). - If IsCallable(promiseResolve) is false, throw a TypeError exception.
- Repeat,
- Let next be IteratorStep(iteratorRecord).
- If next is an abrupt completion, set iteratorRecord.[[Done]] to true.
- ReturnIfAbrupt(next).
- If next is false, then
- Set iteratorRecord.[[Done]] to true.
- Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
- If remainingElementsCount.[[Value]] is 0, then
- Let valuesArray be CreateArrayFromList(values).
- Perform ? Call(resultCapability.[[Resolve]], undefined, « valuesArray »).
- Return resultCapability.[[Promise]].
- Let nextValue be IteratorValue(next).
- If nextValue is an abrupt completion, set iteratorRecord.[[Done]] to true.
- ReturnIfAbrupt(nextValue).
- Append undefined to values.
- Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
- Let steps be the algorithm steps defined in
Promise.allSettled
Resolve Element Functions. - Let resolveElement be ! CreateBuiltinFunction(steps, « [[AlreadyCalled]], [[Index]], [[Values]], [[Capability]], [[RemainingElements]] »).
- Let alreadyCalled be a new Record { [[Value]]: false }.
- Set resolveElement.[[AlreadyCalled]] to alreadyCalled.
- Set resolveElement.[[Index]] to index.
- Set resolveElement.[[Values]] to values.
- Set resolveElement.[[Capability]] to resultCapability.
- Set resolveElement.[[RemainingElements]] to remainingElementsCount.
- Let rejectSteps be the algorithm steps defined in
Promise.allSettled
Reject Element Functions. - Let rejectElement be ! CreateBuiltinFunction(rejectSteps, « [[AlreadyCalled]], [[Index]], [[Values]], [[Capability]], [[RemainingElements]] »).
- Set rejectElement.[[AlreadyCalled]] to alreadyCalled.
- Set rejectElement.[[Index]] to index.
- Set rejectElement.[[Values]] to values.
- Set rejectElement.[[Capability]] to resultCapability.
- Set rejectElement.[[RemainingElements]] to remainingElementsCount.
- Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] + 1.
- Perform ? Invoke(nextPromise,
"then"
, « resolveElement, rejectElement »). - Increase index by 1.
1.2Promise.allSettled
Resolve Element Functions
A Promise.allSettled
resolve element function is an anonymous built-in function that is used to resolve a specific Promise.allSettled
element. Each Promise.allSettled
resolve element function has [[Index]], [[Values]], [[Capability]], [[RemainingElements]], and [[AlreadyCalled]] internal slots.
When a Promise.allSettled
resolve element function is called with argument x, the following steps are taken:
- Let F be the active function object.
- Let alreadyCalled be F.[[AlreadyCalled]].
- If alreadyCalled.[[Value]] is true, return undefined.
- Set alreadyCalled.[[Value]] to true.
- Let index be F.[[Index]].
- Let values be F.[[Values]].
- Let promiseCapability be F.[[Capability]].
- Let remainingElementsCount be F.[[RemainingElements]].
- Let obj be ! ObjectCreate(%ObjectPrototype%).
- Perform ! CreateDataProperty(obj,
"status"
, "fulfilled"
). - Perform ! CreateDataProperty(obj,
"value"
, x). - Set values[index] to be obj.
- Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
- If remainingElementsCount.[[Value]] is 0, then
- Let valuesArray be ! CreateArrayFromList(values).
- Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
- Return undefined.
The "length"
property of a Promise.allSettled
resolve element function is 1.
1.3Promise.allSettled
Reject Element Functions
A Promise.allSettled
reject element function is an anonymous built-in function that is used to reject a specific Promise.allSettled
element. Each Promise.allSettled
reject element function has [[Index]], [[Values]], [[Capability]], [[RemainingElements]], and [[AlreadyCalled]] internal slots.
When a Promise.allSettled
reject element function is called with argument x, the following steps are taken:
- Let F be the active function object.
- Let alreadyCalled be F.[[AlreadyCalled]].
- If alreadyCalled.[[Value]] is true, return undefined.
- Set alreadyCalled.[[Value]] to true.
- Let index be F.[[Index]].
- Let values be F.[[Values]].
- Let promiseCapability be F.[[Capability]].
- Let remainingElementsCount be F.[[RemainingElements]].
- Let obj be ! ObjectCreate(%ObjectPrototype%).
- Perform ! CreateDataProperty(obj,
"status"
, "rejected"
). - Perform ! CreateDataProperty(obj,
"reason"
, x). - Set values[index] to be obj.
- Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
- If remainingElementsCount.[[Value]] is 0, then
- Let valuesArray be CreateArrayFromList(values).
- Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
- Return undefined.
The "length"
property of a Promise.allSettled
reject element function is 1.
ACopyright & Software License
Copyright Notice
© 2019 Jason Williams, Robert Pamely, Mathias Bynens
Software License
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.