How to run a given array of promises in series in JavaScript ?
Last Updated :
15 Feb, 2023
Given an array of Promises, we have to run that in a series. To do this task, we can use then(), to run the next promise, after the completion of a promise.
Approach: The then() method returns a Promise, which helps us to chain promises/methods. The Promise.resolve() method executes the first callback, and when this promise is fulfilled, then it passes to the next function callback1, and it goes on until all the promises are fulfilled. In this way, we can run all the Promises in series.
Syntax:
Promise.resolve( callback0 )
.then( callback1 )
.then( callback2 )
.then( callback3 )...
Example 1: In this example, we will be executing 3 promises by chaining each of them with the then() method.
HTML
<html>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>Promises</b>
<script>
// Define an asynchronous function,
// results in returning a promise
async function task(url) {
console.log(url + " will be fetched now")
return fetch(url);
}
// Declaring an array of URLs
let arr = [
"https://www.google.com",
"https://www.facebook.com",
"https://www.twitter.com"
];
// Resolving the first task
Promise.resolve(() => {
return task(arr[0]);
})
// Resolving the second task
.then(() => {
return task(arr[1]);
})
// Resolving the third task
.then(() => {
return task(arr[2]);
});
</script>
</body>
</html>
Output:
https://www.facebook.com will be fetched now
https://www.twitter.com will be fetched now
The above approach is not feasible if there are a lot more promises in the array. Chaining of the function would get very tiring and will make the code lengthy. We can use the forEach() array function to execute the promises by storing the results in a variable and updating that variable at every promise. This will automatically go through all the promises and one can prevent repeatedly writing then() statements.
Example 2: In this example, we will be executing multiple promises by using the forEach() method.
HTML
<html>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>Promises</b>
<script>
// Define an asynchronous function
async function task(url) {
return fetch(url);
}
// Define array that has to be processed
// by the asynchronous function
let arr = [
"https://www.google.com",
"https://www.facebook.com",
"https://www.twitter.com",
"https://www.youtube.com",
"https://www.netflix.com",
];
// Declare a Promise using its resolve constructor
let promise = Promise.resolve();
// Use the forEach function to evaluate
// the promises in series
// The value of
// p would be the result
// of previous promise
arr.forEach((url, index) => {
promise = promise.then(() => {
let para = document.createElement("p");
para.innerText =
"The async request of " + url +
" has been resolved";
document.body.appendChild(para);
return task(url);
});
});
</script>
</body>
</html>
Output:

Similar Reads
How to access the Value of a Promise in JavaScript In this article, we will see how to access the value of Promise in JavaScript. The Promise is a feature of ES6 introduced in 2015. The concept of Promises is generally used when we want to work asynchronously. The value of a Promise can be accessed in JavaScript using the following methods. Table of
2 min read
How to call promise inside another promise in JavaScript ? In JavaScript, to call promise inside another promise we first declare a promise using its basic syntax and further execute this promise then create another which is to be called inside the previous promise for its execution. Promise is basically a JavaScript object which is responsible for handling
3 min read
JavaScript - How to Handle Errors in Promise.all? To handle errors in Promise.all(), you need to understand that it fails immediately if any of the promises reject, discarding all resolved values. This behavior can lead to incomplete operations and potential application crashes. The best way to manage errors in Promise.all() is by using .catch() in
3 min read
How to delay a loop in JavaScript using async/await with Promise ? In JavaScript, you can delay a loop by using async/await with Promise. By wrapping a setTimeout() inside a Promise and using await, you can pause execution at each iteration, creating delays between loop iterations for asynchronous tasks without blocking the main thread.What is async and await?async
2 min read
How to set Maximum Execution Time for a Promise Await in JavaScript ? In JavaScript, asynchronous operations are more common, often implemented using promises and async/await syntax. However, there are certain scenarios where we want to set the execution times for these asynchronous operations to prevent them from running indefinitely. In this article, we'll explore v
3 min read