| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allKeyed | [Back] [Original] |
Get to know MDN better
The Promise.allKeyed() static method is like Promise.all(), 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 fulfill, with an object of the same keys mapped to the corresponding fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason.
Compared to Promise.all(), Promise.allKeyed() allows you to associate results with semantically meaningful keys, instead of arbitrary array ordering which can be difficult to maintain.
Promise.allKeyed(object)
objectAn object. All of its own enumerable properties, whether the key is a string or a symbol, should have Promise values. These values are awaited, so other thenables are also resolved, while non-thenables are returned as-is.
A Promise that is:
object passed has no own enumerable properties.object fulfill. The fulfillment value is an object of fulfillment values, with the same keys in the same order as the given object, regardless of completion order. If the object passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled.object rejects. The rejection reason is the rejection reason of the first promise that was rejected.The Promise.allKeyed() method is one of the promise concurrency methods. It performs the same kind of task as Promise.all(). However, a lot of the times, you don't already have an array of promises and instead just have a few ad-hoc operations to batch up, so you put them in an array and then immediately destructure:
const [resultA, resultB, resultC] = await Promise.all([getA(), getB(), getC()]);
The problem with this is that you need to maintain the consistency of ordering on both sides: if you accidentally write [resultA, resultC, resultB], your code will break.
The keyed method mitigates the problem by associating each async operation with a semantic key:
const {
a: resultA,
b: resultB,
c: resultC,
} = await Promise.allKeyed({
a: getA(),
b: getB(),
c: getC(),
});
This way, the ordering no longer matters, and any name mismatch is local: accidentally writing b: resultC is now much easier to catch!
The Promise.allKeyed() 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.allKeyed(promises);
console.log(result);
// {
// a: "a",
// [sym]: "symbol",
// nested: {
// b: <Promise>,
// },
// }
For more examples related to concurrency behavior common to Promise.all() and Promise.allKeyed(), see Promise.all().
| Specification |
|---|
| Await Dictionary # sec-promise.allkeyed |
Promise.allKeyed in core-jsPromise.allKeyedPromisePromise.allSettledKeyed()Promise.all()This page was last modified on Aug 28, 2026 by MDN contributors.
PromiseObject/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |