Promise.allSettledKeyed()
The Promise.allSettledKeyed() static method is like Promise.allSettled(), except that instead of using arrays/iterables as input/output, it uses objects. It takes an object where each own key is associated with a promise, and returns a single Promise. This returned promise fulfills when all of the input's promises settle, with an object of the same keys mapped to objects that describe the outcome of the corresponding promise.
Compared to Promise.allSettled(), Promise.allSettledKeyed() allows you to associate results with semantically meaningful keys, instead of arbitrary array ordering which can be difficult to maintain.
Syntax
Promise.allSettledKeyed(object)
Parameters
object-
An object. All of its own enumerable properties, whether the key is a string or a symbol, should have
Promisevalues. These values are awaited, so other thenables are also resolved, while non-thenables are returned as-is.
Return value
A Promise that is:
-
Already fulfilled, if the
objectpassed has no own enumerable properties. -
Asynchronously fulfilled, when all promises in the given
objecthave settled (either fulfilled or rejected). The fulfillment value is an object, with the same keys in the same order as the givenobject, and each property's value being an object describing the outcome of the corresponding promise inobject, regardless of completion order. Each outcome object has the following properties:status-
A string, either
"fulfilled"or"rejected", indicating the eventual state of the promise. value-
Only present if
statusis"fulfilled". The value that the promise was fulfilled with. reason-
Only present if
statusis"rejected". The reason that the promise was rejected with.
If the
objectpassed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled.
Description
The Promise.allSettledKeyed() method is one of the promise concurrency methods. It performs the same kind of task as Promise.allSettled(). Promise.allSettledKeyed() is preferred when you don't already have an array of promises and/or you are immediately destructuring the results; see Promise.allKeyed() for more description.
Examples
>Using Promise.allSettledKeyed()
The Promise.allSettledKeyed() method takes an object and processes all of its own enumerable properties.
function delayed(value, timeout) {
return new Promise((res) => setTimeout(() => res(value), timeout));
}
const sym = Symbol();
const promises = {
a: delayed("a", 500),
// Symbol properties are processed
[sym]: delayed("symbol", 300),
// Nested properties are not processed; this whole object is treated as
// an already-resolved value and returned as-is
nested: {
b: delayed("b", 100),
},
};
const result = await Promise.allSettledKeyed(promises);
console.log(result);
// {
// a: { status: "fulfilled", value: "a" },
// [sym]: { status: "fulfilled", value: "symbol" },
// nested: {
// b: <Promise>,
// },
// }
For more examples related to concurrency behavior common to Promise.allSettled() and Promise.allSettledKeyed(), see Promise.allSettled().
Specifications
| Specification |
|---|
| Await Dictionary> # sec-promise.allsettledkeyed> |