Last Updated : 12 Jul, 2025
The Promise.all() method in JavaScript is used for handling multiple asynchronous operations simultaneously. It takes an array (or any iterable) of promises and returns a single promise that resolves when all the input promises resolve or reject if any one of the promises fails. This makes it ideal for scenarios where you need to wait for multiple asynchronous tasks to complete before proceeding.
Syntax:
Promise.all( iterable )
Parameters:
Return values: It follows some rules to return a single promise:
The below examples illustrate the JavaScript Promise.all() method:
Examples of Using Promise.all() Example 1: Waiting for All Promises to ResolveIn this example the Promise.all waits for all promises (p1, p2, p3) to resolve. It then returns an array of resolved values [50, 200, 'geek'], logging them once all promises are fulfilled.
javascript
p1 = Promise.resolve(50);
p2 = 200
p3 = new Promise(function (resolve, reject) {
setTimeout(resolve, 100, 'geek');
});
Promise.all([p1, p2, p3]).then(function (values) {
console.log(values);
});
[ 50, 200, 'geek' ]Example 2: Handling Promises with Timeouts
In this example the Promise.all waits for all promises (tOut(1000) and tOut(2000)) to resolve. It returns an array of results once both promises are fulfilled, logging [ 'Completed in 1000', 'Completed in 2000' ].
javascript
// Simple promise that resolves
// after a given time
const tOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
// Resolving a normal promise
tOut(1000).then(result => console.log(result + "<br>"))
// Completed in 1000
// Promise.all
Promise.all([tOut(1000), tOut(2000)])
.then(result => console.log(result))
Output:
Completed in 1000 Completed in 1000, Completed in 2000
Here, Promise.all() method is the order of the maintained promises. The first promise in the array will get resolved to the first element of the output array, the second promise will be a second element in the output array, and so on.
Example 3: Array of Promises with Varying TimeoutsIn this example the reates an array of promises with varying timeouts, stores them in promises, and uses Promise.all to resolve them.
javascript
// Simple promise that resolves after a given time
const tOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
// Array contains some time duration
const durations = [1000, 2000, 3000]
const promises = [] // Empty array
durations.map((duration) => {
// Calling the async function timeout(), so
// at this point the async function has started
// and enters the 'pending' state
// pushing the pending promise to an array.
promises.push(tOut(duration))
})
console.log(promises)
// Passing an array of pending promises to Promise.all
// Promise.all will wait till all the promises get resolves
// and then the same gets resolved.
Promise.all(promises).then(response => console.log(response))
// It prints after previous promises gets resolved
// ["Completed in 1000", "Completed in 2000", "Completed in 3000"]
Output :
[object Promise], [object Promise], [object Promise] . . . (gap between previous and last promises) . . Completed in 1000, Completed in 2000, Completed in 3000Example 4: Handling Errors in Promises
In this example, If one of the promises fails, then all the rest of the promises fail and the result will be displayed in the console in the form of an Error. Then Promise.all() method gets rejected.
javascript
// Promise that resolves after a given time
const tOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (t === 2000) {
reject(`Rejected in ${t}`)
} else {
resolve(`Completed in ${t}`)
}
}, t)
})
}
const durations = [1000, 2000, 3000]
// Array contains some time durations
const promises = [] //empty array
durations.map((duration) => {
promises.push(tOut(duration))
// Pushing durations in the promises array
})
// Passing an array of pending promises to Promise.all
Promise.all(promises).then(response => console.log(response))
// Promise.all cannot be resolved, as one of the
// promises passed, got rejected.
.catch(error => console.log(`::Error::<br> ${error}`))
// Promise.all throws an error.
Output :
Error Rejected in 2000Example 5: Using Timers with Different Promises
In this example, we will use some timer function (particularly the setTimeout function) having different timers in it and those will be written inside different promises and further those promises will be passed inside Promise.all() methods in order to obtain the result.
JavaScript
let first_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolved First after 1 second");
}, 1000);
});
let second_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolved First after 2 seconds");
}, 2000);
});
let third_promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Resolved First after 3 seconds");
}, 3000);
});
try {
let result = Promise.all([first_promise, second_promise, third_promise]);
result.then((data) => console.log(data));
} catch (error) {
console.log(error);
}
// This code is contributed by Aman Singla...
Output:
[ 'Resolved First after 1 second', 'Resolved First after 2 seconds', 'Resolved First after 3 seconds' ]Supported Browsers:
The browsers supported by JavaScript Promise.all() methods are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4