🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 53 (from laksa052)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
5 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Last Crawled2026-04-06 05:03:07 (5 days ago)
First Indexed2014-09-16 21:33:58 (11 years ago)
HTTP Status Code200
Meta TitlePromise.all() - JavaScript | MDN
Meta DescriptionThe Promise.all() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason.
Meta Canonicalnull
Boilerpipe Text
Try it const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, "foo"); }); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); }); // Expected output: Array [3, 42, "foo"] Syntax js Promise.all(iterable) Parameters iterable An iterable (such as an Array ) of promises. Return value A Promise that is: Already fulfilled , if the iterable passed is empty. Asynchronously fulfilled , when all the promises in the given iterable fulfill. The fulfillment value is an array of fulfillment values, in the order of the promises passed, regardless of completion order. If the iterable passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled. Asynchronously rejected , when any of the promises in the given iterable rejects. The rejection reason is the rejection reason of the first promise that was rejected. Description The Promise.all() method is one of the promise concurrency methods. It can be useful for aggregating the results of multiple promises. It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues. Promise.all() will reject immediately upon any of the input promises rejecting. In comparison, the promise returned by Promise.allSettled() will wait for all input promises to complete, regardless of whether or not one rejects. Use allSettled() if you need the final result of every promise in the input iterable. Examples Using Promise.all() Promise.all waits for all fulfillments (or the first rejection). js const p1 = Promise.resolve(3); const p2 = 1337; const p3 = new Promise((resolve, reject) => { setTimeout(() => { resolve("foo"); }, 100); }); Promise.all([p1, p2, p3]).then((values) => { console.log(values); // [3, 1337, "foo"] }); If the iterable contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled): js // All values are non-promises, so the returned promise gets fulfilled const p = Promise.all([1, 2, 3]); // The only input promise is already fulfilled, // so the returned promise gets fulfilled const p2 = Promise.all([1, 2, 3, Promise.resolve(444)]); // One (and the only) input promise is rejected, // so the returned promise gets rejected const p3 = Promise.all([1, 2, 3, Promise.reject(new Error("bad"))]); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log(p); console.log(p2); console.log(p3); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[3] } // Promise { <state>: "fulfilled", <value>: Array[4] } // Promise { <state>: "rejected", <reason>: Error: bad } Destructuring the result You will find destructuring very useful if you are batching together a known number of tasks. js // With then() Promise.all([p1, p2, p3]).then(([a, b, c]) => { console.log(a, b, c); // 3 1337 "foo" }); // With await const [a, b, c] = await Promise.all([p1, p2, p3]); Be careful: if the original promises and the result variables' order don't match, you may run into subtle bugs. Asynchronicity or synchronicity of Promise.all This following example demonstrates the asynchronicity of Promise.all when a non-empty iterable is passed: js // Passing an array of promises that are already resolved, // to trigger Promise.all as soon as possible const resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)]; const p = Promise.all(resolvedPromisesArray); // Immediately logging the value of p console.log(p); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs, in order: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } The same thing happens if Promise.all rejects: js const mixedPromisesArray = [ Promise.resolve(33), Promise.reject(new Error("bad")), ]; const p = Promise.all(mixedPromisesArray); console.log(p); setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "rejected", <reason>: Error: bad } Promise.all resolves synchronously if and only if the iterable passed is empty: js const p = Promise.all([]); // Will be immediately resolved const p2 = Promise.all([1337, "hi"]); // Non-promise values are ignored, but the evaluation is done asynchronously console.log(p); console.log(p2); setTimeout(() => { console.log("the queue is now empty"); console.log(p2); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[0] } // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } Using Promise.all() with async functions Within async functions , it's very common to "over-await" your code. For example, given the following functions: js function promptForDishChoice() { return new Promise((resolve, reject) => { const dialog = document.createElement("dialog"); dialog.innerHTML = ` <form method="dialog"> <p>What would you like to eat?</p> <select> <option value="pizza">Pizza</option> <option value="pasta">Pasta</option> <option value="salad">Salad</option> </select> <menu> <li><button value="cancel">Cancel</button></li> <li><button type="submit" value="ok">OK</button></li> </menu> </form> `; dialog.addEventListener("close", () => { if (dialog.returnValue === "ok") { resolve(dialog.querySelector("select").value); } else { reject(new Error("User cancelled dialog")); } }); document.body.appendChild(dialog); dialog.showModal(); }); } async function fetchPrices() { const response = await fetch("/prices"); return await response.json(); } You may write a function like this: js async function getPrice() { const choice = await promptForDishChoice(); const prices = await fetchPrices(); return prices[choice]; } However, note that the execution of promptForDishChoice and fetchPrices don't depend on the result of each other. While the user is choosing their dish, it's fine for the prices to be fetched in the background, but in the code above, the await operator causes the async function to pause until the choice is made, and then again until the prices are fetched. We can use Promise.all to run them concurrently, so that the user doesn't have to wait for the prices to be fetched before the result is given: js async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice(), fetchPrices(), ]); return prices[choice]; } Promise.all is the best choice of concurrency method here, because error handling is intuitive — if any of the promises reject, the result is no longer available, so the whole await expression throws. Promise.all accepts an iterable of promises, so if you are using it to run several async functions concurrently, you need to call the async functions and use the returned promises. Directly passing the functions to Promise.all does not work, since they are not promises. js async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice, fetchPrices, ]); // `choice` and `prices` are still the original async functions; // Promise.all() does nothing to non-promises } Promise.all fail-fast behavior Promise.all is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then Promise.all will reject immediately. js const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("one"), 1000); }); const p2 = new Promise((resolve, reject) => { setTimeout(() => resolve("two"), 2000); }); const p3 = new Promise((resolve, reject) => { setTimeout(() => resolve("three"), 3000); }); const p4 = new Promise((resolve, reject) => { setTimeout(() => resolve("four"), 4000); }); const p5 = new Promise((resolve, reject) => { reject(new Error("reject")); }); // Using .catch: Promise.all([p1, p2, p3, p4, p5]) .then((values) => { console.log(values); }) .catch((error) => { console.error(error.message); }); // Logs: // "reject" It is possible to change this behavior by handling possible rejections: js const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("p1_delayed_resolution"), 1000); }); const p2 = new Promise((resolve, reject) => { reject(new Error("p2_immediate_rejection")); }); Promise.all([p1.catch((error) => error), p2.catch((error) => error)]).then( (values) => { console.log(values[0]); // "p1_delayed_resolution" console.error(values[1]); // "Error: p2_immediate_rejection" }, ); Specifications Specification ECMAScript® 2027 Language Specification # sec-promise.all Browser compatibility See also Promise Promise.allSettled() Promise.any() Promise.race()
Markdown
- [Skip to main content](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#content) - [Skip to search](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#search) HTML [HTML: Markup language](https://developer.mozilla.org/en-US/docs/Web/HTML) HTML reference - [Elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements) - [Global attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes) - [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes) - [See all…](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference "See all HTML references") HTML guides - [Responsive images](https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Responsive_images) - [HTML cheatsheet](https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Cheatsheet) - [Date & time formats](https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Date_and_time_formats) - [See all…](https://developer.mozilla.org/en-US/docs/Web/HTML/Guides "See all HTML guides") Markup languages - [SVG](https://developer.mozilla.org/en-US/docs/Web/SVG) - [MathML](https://developer.mozilla.org/en-US/docs/Web/MathML) - [XML](https://developer.mozilla.org/en-US/docs/Web/XML) CSS [CSS: Styling language](https://developer.mozilla.org/en-US/docs/Web/CSS) CSS reference - [Properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties) - [Selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors) - [At-rules](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules) - [Values](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values) - [See all…](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference "See all CSS references") CSS guides - [Box model](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Box_model/Introduction) - [Animations](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Animations/Using) - [Flexbox](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Flexible_box_layout/Basic_concepts) - [Colors](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Colors/Applying_color) - [See all…](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides "See all CSS guides") Layout cookbook - [Column layouts](https://developer.mozilla.org/en-US/docs/Web/CSS/How_to/Layout_cookbook/Column_layouts) - [Centering an element](https://developer.mozilla.org/en-US/docs/Web/CSS/How_to/Layout_cookbook/Center_an_element) - [Card component](https://developer.mozilla.org/en-US/docs/Web/CSS/How_to/Layout_cookbook/Card) - [See all…](https://developer.mozilla.org/en-US/docs/Web/CSS/How_to/Layout_cookbook) JavaScriptJS [JavaScript: Scripting language](https://developer.mozilla.org/en-US/docs/Web/JavaScript) JS reference - [Standard built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) - [Expressions & operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators) - [Statements & declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) - [Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions) - [See all…](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference "See all JavaScript references") JS guides - [Control flow & error handing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling) - [Loops and iteration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration) - [Working with objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects) - [Using classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_classes) - [See all…](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide "See all JavaScript guides") Web APIs [Web APIs: Programming interfaces](https://developer.mozilla.org/en-US/docs/Web/API) Web API reference - [File system API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API) - [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) - [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) - [HTML DOM API](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API) - [Push API](https://developer.mozilla.org/en-US/docs/Web/API/Push_API) - [Service worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) - [See all…](https://developer.mozilla.org/en-US/docs/Web/API "See all Web API guides") Web API guides - [Using the Web animation API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API) - [Using the Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) - [Working with the History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API/Working_with_the_History_API) - [Using the Web speech API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API/Using_the_Web_Speech_API) - [Using web workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) All [All web technology](https://developer.mozilla.org/en-US/docs/Web) Technologies - [Accessibility](https://developer.mozilla.org/en-US/docs/Web/Accessibility) - [HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP) - [URI](https://developer.mozilla.org/en-US/docs/Web/URI) - [Web extensions](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions) - [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly) - [WebDriver](https://developer.mozilla.org/en-US/docs/Web/WebDriver) - [See all…](https://developer.mozilla.org/en-US/docs/Web "See all web technology references") Topics - [Media](https://developer.mozilla.org/en-US/docs/Web/Media) - [Performance](https://developer.mozilla.org/en-US/docs/Web/Performance) - [Privacy](https://developer.mozilla.org/en-US/docs/Web/Privacy) - [Security](https://developer.mozilla.org/en-US/docs/Web/Security) - [Progressive web apps](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps) Learn [Learn web development](https://developer.mozilla.org/en-US/docs/Learn_web_development) Frontend developer course - [Getting started modules](https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started) - [Core modules](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core) - [MDN Curriculum](https://developer.mozilla.org/en-US/curriculum/) - [Check out the video course from Scrimba, our partner](https://scrimba.com/frontend-path-c0j?via=mdn-learn-navbar) Learn HTML - [Structuring content with HTML module](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content) Learn CSS - [CSS styling basics module](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics) - [CSS layout module](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/CSS_layout) Learn JavaScript - [Dynamic scripting with JavaScript module](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting) Tools Discover our tools - [Playground](https://developer.mozilla.org/en-US/play) - [HTTP Observatory](https://developer.mozilla.org/en-US/observatory) - [Border-image generator](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Backgrounds_and_borders/Border-image_generator) - [Border-radius generator](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Backgrounds_and_borders/Border-radius_generator) - [Box-shadow generator](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Backgrounds_and_borders/Box-shadow_generator) - [Color format converter](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Colors/Color_format_converter) - [Color mixer](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Colors/Color_mixer) - [Shape generator](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Shapes/Shape_generator) About Get to know MDN better - [About MDN](https://developer.mozilla.org/en-US/about) - [Advertise with us](https://developer.mozilla.org/en-US/advertising) - [Community](https://developer.mozilla.org/en-US/community) - [MDN on GitHub](https://github.com/mdn) [Blog](https://developer.mozilla.org/en-US/blog/) 1. [Web](https://developer.mozilla.org/en-US/docs/Web) 2. [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) 3. [Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) 4. [Standard built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) 5. [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 6. [all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) # Promise.all() Baseline Widely available This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015. - [Learn more](https://developer.mozilla.org/en-US/docs/Glossary/Baseline/Compatibility) - [See full compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#browser_compatibility) - [Report feedback](https://survey.alchemer.com/s3/7634825/MDN-baseline-feedback?page=%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FPromise%2Fall&level=high) The **`Promise.all()`** static method takes an iterable of promises as input and returns a single [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason. ## In this article - [Try it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#try_it) - [Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#syntax) - [Description](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#description) - [Examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#examples) - [Specifications](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#specifications) - [Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#browser_compatibility) - [See also](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#see_also) ## [Try it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#try_it) ``` const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, "foo"); }); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); }); // Expected output: Array [3, 42, "foo"] ``` ## [Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#syntax) js ``` Promise.all(iterable) ``` ### [Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#parameters) [`iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#iterable) An [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) (such as an [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)) of promises. ### [Return value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#return_value) A [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that is: - **Already fulfilled**, if the `iterable` passed is empty. - **Asynchronously fulfilled**, when all the promises in the given `iterable` fulfill. The fulfillment value is an array of fulfillment values, in the order of the promises passed, regardless of completion order. If the `iterable` passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled. - **Asynchronously rejected**, when any of the promises in the given `iterable` rejects. The rejection reason is the rejection reason of the first promise that was rejected. ## [Description](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#description) The `Promise.all()` method is one of the [promise concurrency](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency) methods. It can be useful for aggregating the results of multiple promises. It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues. `Promise.all()` will reject immediately upon **any** of the input promises rejecting. In comparison, the promise returned by [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) will wait for all input promises to complete, regardless of whether or not one rejects. Use `allSettled()` if you need the final result of every promise in the input iterable. ## [Examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#examples) ### [Using Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#using_promise.all) `Promise.all` waits for all fulfillments (or the first rejection). js ``` const p1 = Promise.resolve(3); const p2 = 1337; const p3 = new Promise((resolve, reject) => { setTimeout(() => { resolve("foo"); }, 100); }); Promise.all([p1, p2, p3]).then((values) => { console.log(values); // [3, 1337, "foo"] }); ``` If the `iterable` contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled): js ``` // All values are non-promises, so the returned promise gets fulfilled const p = Promise.all([1, 2, 3]); // The only input promise is already fulfilled, // so the returned promise gets fulfilled const p2 = Promise.all([1, 2, 3, Promise.resolve(444)]); // One (and the only) input promise is rejected, // so the returned promise gets rejected const p3 = Promise.all([1, 2, 3, Promise.reject(new Error("bad"))]); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log(p); console.log(p2); console.log(p3); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[3] } // Promise { <state>: "fulfilled", <value>: Array[4] } // Promise { <state>: "rejected", <reason>: Error: bad } ``` ### [Destructuring the result](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#destructuring_the_result) You will find [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring) very useful if you are batching together a known number of tasks. js ``` // With then() Promise.all([p1, p2, p3]).then(([a, b, c]) => { console.log(a, b, c); // 3 1337 "foo" }); // With await const [a, b, c] = await Promise.all([p1, p2, p3]); ``` Be careful: if the original promises and the result variables' order don't match, you may run into subtle bugs. ### [Asynchronicity or synchronicity of Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#asynchronicity_or_synchronicity_of_promise.all) This following example demonstrates the asynchronicity of `Promise.all` when a non-empty `iterable` is passed: js ``` // Passing an array of promises that are already resolved, // to trigger Promise.all as soon as possible const resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)]; const p = Promise.all(resolvedPromisesArray); // Immediately logging the value of p console.log(p); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs, in order: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } ``` The same thing happens if `Promise.all` rejects: js ``` const mixedPromisesArray = [ Promise.resolve(33), Promise.reject(new Error("bad")), ]; const p = Promise.all(mixedPromisesArray); console.log(p); setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "rejected", <reason>: Error: bad } ``` `Promise.all` resolves synchronously if and only if the `iterable` passed is empty: js ``` const p = Promise.all([]); // Will be immediately resolved const p2 = Promise.all([1337, "hi"]); // Non-promise values are ignored, but the evaluation is done asynchronously console.log(p); console.log(p2); setTimeout(() => { console.log("the queue is now empty"); console.log(p2); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[0] } // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } ``` ### [Using Promise.all() with async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#using_promise.all_with_async_functions) Within [async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), it's very common to "over-await" your code. For example, given the following functions: js ``` function promptForDishChoice() { return new Promise((resolve, reject) => { const dialog = document.createElement("dialog"); dialog.innerHTML = ` <form method="dialog"> <p>What would you like to eat?</p> <select> <option value="pizza">Pizza</option> <option value="pasta">Pasta</option> <option value="salad">Salad</option> </select> <menu> <li><button value="cancel">Cancel</button></li> <li><button type="submit" value="ok">OK</button></li> </menu> </form> `; dialog.addEventListener("close", () => { if (dialog.returnValue === "ok") { resolve(dialog.querySelector("select").value); } else { reject(new Error("User cancelled dialog")); } }); document.body.appendChild(dialog); dialog.showModal(); }); } async function fetchPrices() { const response = await fetch("/prices"); return await response.json(); } ``` You may write a function like this: js ``` async function getPrice() { const choice = await promptForDishChoice(); const prices = await fetchPrices(); return prices[choice]; } ``` However, note that the execution of `promptForDishChoice` and `fetchPrices` don't depend on the result of each other. While the user is choosing their dish, it's fine for the prices to be fetched in the background, but in the code above, the [`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) operator causes the async function to pause until the choice is made, and then again until the prices are fetched. We can use `Promise.all` to run them concurrently, so that the user doesn't have to wait for the prices to be fetched before the result is given: js ``` async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice(), fetchPrices(), ]); return prices[choice]; } ``` `Promise.all` is the best choice of [concurrency method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency) here, because error handling is intuitive — if any of the promises reject, the result is no longer available, so the whole `await` expression throws. `Promise.all` accepts an iterable of promises, so if you are using it to run several async functions concurrently, you need to call the async functions and use the returned promises. Directly passing the functions to `Promise.all` does not work, since they are not promises. js ``` async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice, fetchPrices, ]); // `choice` and `prices` are still the original async functions; // Promise.all() does nothing to non-promises } ``` ### [Promise.all fail-fast behavior](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#promise.all_fail-fast_behavior) `Promise.all` is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then `Promise.all` will reject immediately. js ``` const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("one"), 1000); }); const p2 = new Promise((resolve, reject) => { setTimeout(() => resolve("two"), 2000); }); const p3 = new Promise((resolve, reject) => { setTimeout(() => resolve("three"), 3000); }); const p4 = new Promise((resolve, reject) => { setTimeout(() => resolve("four"), 4000); }); const p5 = new Promise((resolve, reject) => { reject(new Error("reject")); }); // Using .catch: Promise.all([p1, p2, p3, p4, p5]) .then((values) => { console.log(values); }) .catch((error) => { console.error(error.message); }); // Logs: // "reject" ``` It is possible to change this behavior by handling possible rejections: js ``` const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("p1_delayed_resolution"), 1000); }); const p2 = new Promise((resolve, reject) => { reject(new Error("p2_immediate_rejection")); }); Promise.all([p1.catch((error) => error), p2.catch((error) => error)]).then( (values) => { console.log(values[0]); // "p1_delayed_resolution" console.error(values[1]); // "Error: p2_immediate_rejection" }, ); ``` ## [Specifications](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#specifications) | Specification | |---| | [ECMAScript® 2027 Language Specification \# sec-promise.all](https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise.all) | ## [Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#browser_compatibility) ## [See also](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#see_also) - [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) - [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) - [`Promise.any()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) - [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) ## Help improve MDN [Learn how to contribute](https://developer.mozilla.org/en-US/docs/MDN/Community/Getting_started) This page was last modified on Aug 19, 2025 by [MDN contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all/contributors.txt). [View this page on GitHub](https://github.com/mdn/content/blob/main/files/en-us/web/javascript/reference/global_objects/promise/all/index.md?plain=1 "Folder: en-us/web/javascript/reference/global_objects/promise/all (Opens in a new tab)") • [Report a problem with this content](https://github.com/mdn/content/issues/new?template=page-report.yml&mdn-url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FPromise%2Fall&metadata=%3C%21--+Do+not+make+changes+below+this+line+--%3E%0A%3Cdetails%3E%0A%3Csummary%3EPage+report+details%3C%2Fsummary%3E%0A%0A*+Folder%3A+%60en-us%2Fweb%2Fjavascript%2Freference%2Fglobal_objects%2Fpromise%2Fall%60%0A*+MDN+URL%3A+https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FPromise%2Fall%0A*+GitHub+URL%3A+https%3A%2F%2Fgithub.com%2Fmdn%2Fcontent%2Fblob%2Fmain%2Ffiles%2Fen-us%2Fweb%2Fjavascript%2Freference%2Fglobal_objects%2Fpromise%2Fall%2Findex.md%0A*+Last+commit%3A+https%3A%2F%2Fgithub.com%2Fmdn%2Fcontent%2Fcommit%2F3fad0447b4901e28fe88769976787d8d8b87d66d%0A*+Document+last+modified%3A+2025-08-19T22%3A52%3A50.000Z%0A%0A%3C%2Fdetails%3E "This will take you to GitHub to file a new issue.") 1. [Standard built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) 2. [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 3. Constructor 1. [`Promise()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) 4. Static methods 1. *[`all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)* 2. [`allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) 3. [`any()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) 4. [`race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) 5. [`reject()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject) 6. [`resolve()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve) 7. [`try()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/try) 8. [`withResolvers()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers) 5. Static properties 1. [`[Symbol.species]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Symbol.species) 6. Instance methods 1. [`catch()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) 2. [`finally()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally) 3. [`then()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) 7. Inheritance 8. [`Object/Function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) 9. Static methods 1. [`apply()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) 2. [`bind()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) 3. [`call()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) 4. [`toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString) 5. [`[Symbol.hasInstance]()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Symbol.hasInstance) 10. Static properties 1. [`displayName`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/displayName) Non-standard 2. [`length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) 3. [`name`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) 4. [`prototype`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) 5. [`arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/arguments) Non-standard Deprecated 6. [`caller`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller) Non-standard Deprecated 11. Instance methods 1. [`__defineGetter__()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__) Deprecated 2. [`__defineSetter__()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineSetter__) Deprecated 3. [`__lookupGetter__()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__lookupGetter__) Deprecated 4. [`__lookupSetter__()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__lookupSetter__) Deprecated 5. [`hasOwnProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) 6. [`isPrototypeOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf) 7. [`propertyIsEnumerable()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable) 8. [`toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString) 9. [`toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) 10. [`valueOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf) 12. Instance properties 1. [`__proto__`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto) Deprecated 2. [`constructor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor) Your blueprint for a better internet. MDN - [About](https://developer.mozilla.org/en-US/about) - [Blog](https://developer.mozilla.org/en-US/blog/) - [Mozilla careers](https://www.mozilla.org/en-US/careers/listings/) - [Advertise with us](https://developer.mozilla.org/en-US/advertising) - [MDN Plus](https://developer.mozilla.org/en-US/plus) - [Product help](https://support.mozilla.org/products/mdn-plus) Contribute - [MDN Community](https://developer.mozilla.org/en-US/community) - [Community resources](https://developer.mozilla.org/en-US/docs/MDN/Community) - [Writing guidelines](https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines) - [MDN Discord](https://developer.mozilla.org/discord) - [MDN on GitHub](https://github.com/mdn) Developers - [Web technologies](https://developer.mozilla.org/en-US/docs/Web) - [Learn web development](https://developer.mozilla.org/en-US/docs/Learn_web_development) - [Guides](https://developer.mozilla.org/en-US/docs/MDN/Guides) - [Tutorials](https://developer.mozilla.org/en-US/docs/MDN/Tutorials) - [Glossary](https://developer.mozilla.org/en-US/docs/Glossary) - [Hacks blog](https://hacks.mozilla.org/) - [Website Privacy Notice](https://www.mozilla.org/privacy/websites/) - [Telemetry Settings](https://www.mozilla.org/en-US/privacy/websites/data-preferences/) - [Legal](https://www.mozilla.org/about/legal/terms/mozilla) - [Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/) Visit [Mozilla Corporation’s](https://www.mozilla.org/) not-for-profit parent, the [Mozilla Foundation](https://foundation.mozilla.org/). Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under [a Creative Commons license](https://developer.mozilla.org/docs/MDN/Writing_guidelines/Attrib_copyright_license).
Readable Markdown
## [Try it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#try_it) ``` const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, "foo"); }); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); }); // Expected output: Array [3, 42, "foo"] ``` ## [Syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#syntax) js ``` Promise.all(iterable) ``` ### [Parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#parameters) [`iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#iterable) An [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) (such as an [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)) of promises. ### [Return value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#return_value) A [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that is: - **Already fulfilled**, if the `iterable` passed is empty. - **Asynchronously fulfilled**, when all the promises in the given `iterable` fulfill. The fulfillment value is an array of fulfillment values, in the order of the promises passed, regardless of completion order. If the `iterable` passed is non-empty but contains no pending promises, the returned promise is still asynchronously (instead of synchronously) fulfilled. - **Asynchronously rejected**, when any of the promises in the given `iterable` rejects. The rejection reason is the rejection reason of the first promise that was rejected. ## [Description](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#description) The `Promise.all()` method is one of the [promise concurrency](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency) methods. It can be useful for aggregating the results of multiple promises. It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues. `Promise.all()` will reject immediately upon **any** of the input promises rejecting. In comparison, the promise returned by [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) will wait for all input promises to complete, regardless of whether or not one rejects. Use `allSettled()` if you need the final result of every promise in the input iterable. ## [Examples](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#examples) ### [Using Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#using_promise.all) `Promise.all` waits for all fulfillments (or the first rejection). js ``` const p1 = Promise.resolve(3); const p2 = 1337; const p3 = new Promise((resolve, reject) => { setTimeout(() => { resolve("foo"); }, 100); }); Promise.all([p1, p2, p3]).then((values) => { console.log(values); // [3, 1337, "foo"] }); ``` If the `iterable` contains non-promise values, they will be ignored, but still counted in the returned promise array value (if the promise is fulfilled): js ``` // All values are non-promises, so the returned promise gets fulfilled const p = Promise.all([1, 2, 3]); // The only input promise is already fulfilled, // so the returned promise gets fulfilled const p2 = Promise.all([1, 2, 3, Promise.resolve(444)]); // One (and the only) input promise is rejected, // so the returned promise gets rejected const p3 = Promise.all([1, 2, 3, Promise.reject(new Error("bad"))]); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log(p); console.log(p2); console.log(p3); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[3] } // Promise { <state>: "fulfilled", <value>: Array[4] } // Promise { <state>: "rejected", <reason>: Error: bad } ``` ### [Destructuring the result](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#destructuring_the_result) You will find [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring) very useful if you are batching together a known number of tasks. js ``` // With then() Promise.all([p1, p2, p3]).then(([a, b, c]) => { console.log(a, b, c); // 3 1337 "foo" }); // With await const [a, b, c] = await Promise.all([p1, p2, p3]); ``` Be careful: if the original promises and the result variables' order don't match, you may run into subtle bugs. ### [Asynchronicity or synchronicity of Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#asynchronicity_or_synchronicity_of_promise.all) This following example demonstrates the asynchronicity of `Promise.all` when a non-empty `iterable` is passed: js ``` // Passing an array of promises that are already resolved, // to trigger Promise.all as soon as possible const resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)]; const p = Promise.all(resolvedPromisesArray); // Immediately logging the value of p console.log(p); // Using setTimeout, we can execute code after the queue is empty setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs, in order: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } ``` The same thing happens if `Promise.all` rejects: js ``` const mixedPromisesArray = [ Promise.resolve(33), Promise.reject(new Error("bad")), ]; const p = Promise.all(mixedPromisesArray); console.log(p); setTimeout(() => { console.log("the queue is now empty"); console.log(p); }); // Logs: // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "rejected", <reason>: Error: bad } ``` `Promise.all` resolves synchronously if and only if the `iterable` passed is empty: js ``` const p = Promise.all([]); // Will be immediately resolved const p2 = Promise.all([1337, "hi"]); // Non-promise values are ignored, but the evaluation is done asynchronously console.log(p); console.log(p2); setTimeout(() => { console.log("the queue is now empty"); console.log(p2); }); // Logs: // Promise { <state>: "fulfilled", <value>: Array[0] } // Promise { <state>: "pending" } // the queue is now empty // Promise { <state>: "fulfilled", <value>: Array[2] } ``` ### [Using Promise.all() with async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#using_promise.all_with_async_functions) Within [async functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), it's very common to "over-await" your code. For example, given the following functions: js ``` function promptForDishChoice() { return new Promise((resolve, reject) => { const dialog = document.createElement("dialog"); dialog.innerHTML = ` <form method="dialog"> <p>What would you like to eat?</p> <select> <option value="pizza">Pizza</option> <option value="pasta">Pasta</option> <option value="salad">Salad</option> </select> <menu> <li><button value="cancel">Cancel</button></li> <li><button type="submit" value="ok">OK</button></li> </menu> </form> `; dialog.addEventListener("close", () => { if (dialog.returnValue === "ok") { resolve(dialog.querySelector("select").value); } else { reject(new Error("User cancelled dialog")); } }); document.body.appendChild(dialog); dialog.showModal(); }); } async function fetchPrices() { const response = await fetch("/prices"); return await response.json(); } ``` You may write a function like this: js ``` async function getPrice() { const choice = await promptForDishChoice(); const prices = await fetchPrices(); return prices[choice]; } ``` However, note that the execution of `promptForDishChoice` and `fetchPrices` don't depend on the result of each other. While the user is choosing their dish, it's fine for the prices to be fetched in the background, but in the code above, the [`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) operator causes the async function to pause until the choice is made, and then again until the prices are fetched. We can use `Promise.all` to run them concurrently, so that the user doesn't have to wait for the prices to be fetched before the result is given: js ``` async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice(), fetchPrices(), ]); return prices[choice]; } ``` `Promise.all` is the best choice of [concurrency method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency) here, because error handling is intuitive — if any of the promises reject, the result is no longer available, so the whole `await` expression throws. `Promise.all` accepts an iterable of promises, so if you are using it to run several async functions concurrently, you need to call the async functions and use the returned promises. Directly passing the functions to `Promise.all` does not work, since they are not promises. js ``` async function getPrice() { const [choice, prices] = await Promise.all([ promptForDishChoice, fetchPrices, ]); // `choice` and `prices` are still the original async functions; // Promise.all() does nothing to non-promises } ``` ### [Promise.all fail-fast behavior](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#promise.all_fail-fast_behavior) `Promise.all` is rejected if any of the elements are rejected. For example, if you pass in four promises that resolve after a timeout and one promise that rejects immediately, then `Promise.all` will reject immediately. js ``` const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("one"), 1000); }); const p2 = new Promise((resolve, reject) => { setTimeout(() => resolve("two"), 2000); }); const p3 = new Promise((resolve, reject) => { setTimeout(() => resolve("three"), 3000); }); const p4 = new Promise((resolve, reject) => { setTimeout(() => resolve("four"), 4000); }); const p5 = new Promise((resolve, reject) => { reject(new Error("reject")); }); // Using .catch: Promise.all([p1, p2, p3, p4, p5]) .then((values) => { console.log(values); }) .catch((error) => { console.error(error.message); }); // Logs: // "reject" ``` It is possible to change this behavior by handling possible rejections: js ``` const p1 = new Promise((resolve, reject) => { setTimeout(() => resolve("p1_delayed_resolution"), 1000); }); const p2 = new Promise((resolve, reject) => { reject(new Error("p2_immediate_rejection")); }); Promise.all([p1.catch((error) => error), p2.catch((error) => error)]).then( (values) => { console.log(values[0]); // "p1_delayed_resolution" console.error(values[1]); // "Error: p2_immediate_rejection" }, ); ``` ## [Specifications](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#specifications) | Specification | |---| | [ECMAScript® 2027 Language Specification \# sec-promise.all](https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise.all) | ## [Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#browser_compatibility) ## [See also](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#see_also) - [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) - [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) - [`Promise.any()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) - [`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)
Shard53 (laksa)
Root Hash7082249407640205653
Unparsed URLorg,mozilla!developer,/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all s443