Introduction
Promise.all() is a built-in JavaScript method that allows you to execute multiple Promises simultaneously and wait until all of them complete successfully.
Instead of executing asynchronous tasks one after another, Promise.all() runs them in parallel, making applications faster and more efficient.
If every Promise is fulfilled, Promise.all() returns an array containing the results in the same order as the Promises were provided.
If any one Promise is rejected, Promise.all() immediately rejects with that error, and the remaining results are ignored.
For automation engineers, Promise.all() is extremely useful when multiple independent tasks can be executed at the same time, such as loading multiple files, sending multiple API requests, validating several UI elements, or retrieving data from different sources.
In this tutorial, you’ll learn how Promise.all() works and how to use it in Node.js applications.
What is Promise.all()?
Promise.all() is a static method of the Promise object.
It accepts an array of Promises and returns a single Promise.
The returned Promise:
Resolves when all Promises succeed.
Rejects immediately if any Promise fails.
Why Use Promise.all()?
Promise.all() helps developers:
Execute multiple tasks in parallel.
Improve application performance.
Reduce waiting time.
Collect multiple results together.
Simplify asynchronous code.
Improve scalability.
Handle concurrent operations efficiently.
Syntax
Promise.all([
promise1,
promise2,
promise3
])
.then(function (results) {
console.log(results);
})
.catch(function (error) {
console.log(error);
});
Example 1: Basic Promise.all()
const promise1 =
Promise.resolve(10);
const promise2 =
Promise.resolve(20);
Promise.all([
promise1,
promise2
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 10, 20 ]
Example 2: Multiple String Results
Promise.all([
Promise.resolve("Node.js"),
Promise.resolve("Playwright"),
Promise.resolve("Automation")
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Node.js', 'Playwright', 'Automation' ]
Example 3: Using setTimeout()
const promise1 =
new Promise(function (resolve) {
setTimeout(function () {
resolve("Task 1");
}, 1000);
});
const promise2 =
new Promise(function (resolve) {
setTimeout(function () {
resolve("Task 2");
}, 2000);
});
Promise.all([
promise1,
promise2
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Task 1', 'Task 2' ]
Example 4: Handling Rejection
const promise1 =
Promise.resolve("Success");
const promise2 =
Promise.reject("Failure");
Promise.all([
promise1,
promise2
])
.catch(function (error) {
console.log(error);
});
Sample Output
Failure
Example 5: Reading Multiple Files
const fs =
require("fs").promises;
Promise.all([
fs.readFile(
"file1.txt",
"utf8"
),
fs.readFile(
"file2.txt",
"utf8"
)
])
.then(function (files) {
console.log(files);
})
.catch(function (error) {
console.log(error);
});
The files are read concurrently, improving performance.
Automation Testing Examples
Promise.all() is frequently used in automation frameworks to perform multiple independent tasks simultaneously.
Playwright Example
Wait for multiple pages to finish loading.
Promise.all([
Promise.resolve(
"Home loaded."
),
Promise.resolve(
"Dashboard loaded."
)
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Home loaded.', 'Dashboard loaded.' ]
Selenium Example
Start multiple browser sessions.
Promise.all([
Promise.resolve("Chrome"),
Promise.resolve("Firefox")
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Chrome', 'Firefox' ]
Cypress Example
Validate multiple page elements.
Promise.all([
Promise.resolve("Logo"),
Promise.resolve("Menu"),
Promise.resolve("Footer")
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Logo', 'Menu', 'Footer' ]
API Testing Example
Send multiple API requests.
Promise.all([
Promise.resolve("Users"),
Promise.resolve("Products"),
Promise.resolve("Orders")
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'Users', 'Products', 'Orders' ]
Data-Driven Testing Example
Load multiple data files.
Promise.all([
Promise.resolve("users.csv"),
Promise.resolve("employees.csv"),
Promise.resolve("products.csv")
])
.then(function (results) {
console.log(results);
});
Sample Output
[ 'users.csv', 'employees.csv', 'products.csv' ]
Common Uses of Promise.all()
Promise.all() is commonly used for:
Reading multiple files.
Sending parallel API requests.
Database queries.
Browser automation.
Loading configuration files.
Image uploads.
Data processing.
Fetching multiple resources.
Running independent tasks.
Batch operations.
Promise.all() vs Promise Chaining
| Feature | Promise.all() | Promise Chaining |
|---|---|---|
| Execution | Parallel | Sequential |
| Speed | Faster for independent tasks | Better for dependent tasks |
| Result | Array of results | One result at a time |
| Failure | Rejects immediately if one Promise fails | Errors handled during the chain |
| Best Use Case | Independent asynchronous operations | Dependent asynchronous operations |
Common Mistakes
Using Promise.all() for Dependent Tasks
Use Promise chaining or async/await when one task depends on the result of another.
Ignoring Rejections
Always use .catch() because a single rejected Promise causes the entire Promise.all() operation to fail.
Assuming Results Arrive in Completion Order
The results are returned in the same order as the input Promises, regardless of which Promise finishes first.
Best Practices
Use
Promise.all()for independent asynchronous tasks.Always handle errors using
.catch().Keep related Promises together.
Avoid using it for dependent operations.
Use descriptive variable names.
Minimize unnecessary Promise creation.
Prefer
async/awaitwithPromise.all()for cleaner code in modern applications.
Conclusion
Promise.all() is a powerful method for executing multiple asynchronous operations concurrently. It improves application performance by running independent tasks in parallel and returning all successful results together.
For automation engineers, Promise.all() is especially useful when validating multiple UI elements, sending parallel API requests, loading several files, or performing concurrent test setup tasks. Understanding Promise.all() will help you build faster, more efficient, and scalable Node.js automation frameworks.
Frequently Asked Questions (FAQs)
What does Promise.all() do?
Promise.all() executes multiple Promises concurrently and resolves when all of them complete successfully.
What happens if one Promise fails?
The returned Promise is rejected immediately with that error.
Does Promise.all() preserve the order of results?
Yes. Results are returned in the same order as the input Promises, regardless of completion time.
When should I use Promise.all()?
Use it when multiple asynchronous tasks are independent and can run at the same time.
Why is Promise.all() useful in automation testing?
It allows multiple independent tasks such as API requests, UI validations, and file loading to execute concurrently, reducing overall test execution time.
Key Takeaways
Promise.all()runs multiple Promises concurrently.It resolves only when all Promises succeed.
A single rejected Promise causes the entire operation to fail.
Results are returned as an array.
The result order matches the input Promise order.
Promise.all()improves performance for independent tasks.It is widely used in Node.js applications.
Automation frameworks use it for parallel execution.
Always handle errors using
.catch().Use Promise chaining or
async/awaitfor dependent operations instead ofPromise.all().
