- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run a given array of promises in series in JavaScript?
In JavaScript, there is a method called "Promise.all" that allows you to run an array of promises in parallel. However, sometimes you may want to run your promises in series instead. This can be useful if you want to make sure that each promise is executed one after the other, or if you need to use the result of one promise in the execution of the next promise.
There are a few different ways that you can run an array of promises in series in JavaScript. In this article, we'll take a look at a few of them.
Promise.prototype.then()
One way to run an array of promises in series is to chain them together using the then() method. This method takes a function as its input, which will be executed after the promise is fulfilled.
Example
<html> <head> <title>Examples</title> </head> <body> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <script> Promise.resolve(1) .then(result => { document.getElementById("result1").innerHTML = result return Promise.resolve(2); }) .then(result => { document.getElementById("result2").innerHTML = result return Promise.resolve(3); }) .then(result => { document.getElementById("result3").innerHTML = result }); </script> </body> </html>
As you can see, we're using the then() method to chain together three promises. The first promise resolves to the value 1, which is displayed. The second promise resolves to the value 2, which is also displayed. And finally, the third promise resolves to the value 3, which is displayed.
Because the "then" method returns a promise, we can chain promises together in this way to create a series.
for-await-of
Another way to run an array of promises in series is to use the "for-await-of" loop. This loop allows you to use the await keyword inside of a for loop. The await keyword pauses the execution of the code until the promise is fulfilled.
Example
Here's an example −
<html> <head> <title>Example- for-await-of</title> </head> <body> <script> async function runPromisesInSeries() { for await (const promise of [ Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), ]) { const result = await promise; document.write(result); document.write("<br>") } } runPromisesInSeries(); </script> </body> </html>
In this example, we have an async function that contains a "for-await-of" loop. This loop iterates over an array of promises. For each promise in the array, we await the promise to be fulfilled. Once the promise is fulfilled, the value is displayed.
Using a library
If you need more functionality than what's provided by the native Promise API, you can use a library like Bluebird or Q. These libraries provide additional methods for working with promises.
For example, Bluebird provides a "map" method that allows you to map an array of values to an array of promises, and then wait for all of the promises to be fulfilled −
const Promise = require('bluebird'); Promise.map([1, 2, 3], x => { return Promise.resolve(x * 2); }).then(results => { console.log(results); // [2, 4, 6] });
Conclusion
In this article, we've seen a few different ways that you can run an array of promises in series in JavaScript. We've seen how to use the "then" method to chain promises together, how to use the "for-await-of" loop, and how to use a library like Bluebird or Q.
- Related Articles
- JavaScript Promises
- What are Promises in JavaScript?
- How to execute a cube of numbers of a given array in JavaScript?
- How to find the sum of all elements of a given array in JavaScript?
- How To Run JavaScript Online?
- Resolving or rejecting promises accordingly - JavaScript
- Create empty array of a given size in JavaScript
- How to find and return the longest repeating series of numbers in array with JavaScript
- How to create permutation of array with the given number of elements in JavaScript
- How to run functions iteratively with async await in JavaScript?
- How to count the number of elements in an array below/above a given number (JavaScript)
- How to calculate the average in JavaScript of the given properties in the array of objects
- What are the differences between Deferreds, Promises and Futures in javascript?
- How to view array of a structure in JavaScript?
- Write a program in Python to print numeric index array with sorted distinct values in a given series
