How to run a given array of promises in series in JavaScript ? Last Updated : 15 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article How to run a given array of promises in series in JavaScript ? M ManishKhetan Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions 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 How to Convert Callback to Promise in JavaScript ? Asynchronous programming in JavaScript often involves the use of callbacks. However, callbacks can lead to callback hell and make the code harder to read and maintain. Promises provide a cleaner way to handle asynchronous operations. Converting existing callback-based code to use promises can improv 2 min read How to convert an asynchronous function to return a promise in JavaScript ? In this article, we will learn how to convert an asynchronous function to return a promise in JavaScript. Approach:Â You need to first declare a simple function (either a normal function or an arrow function (which is preferred)). You need to create an asynchronous function and then further you need 3 min read How to Push an Object into an Array using For Loop in JavaScript ? JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to pu 3 min read Explain Promise.all with async-await in JavaScript In JavaScript, Promise.all with async-await is used to handle multiple asynchronous operations concurrently. By combining Promise.all with await, you can wait for all promises to resolve or any to reject, ensuring that all asynchronous tasks are complete before proceeding with the next code executio 4 min read Explain Promise.any() with async-await in JavaScript In this article, we will try to understand how to implement the Promise.any() method with async-await in JavaScript using some theoretical explanations followed by some coding examples as well. Let us firstly quickly understand the working of Promise.any() method with some theoretical examples (incl 4 min read Like