Promise.all() and map() with Async/Await by Example
In this post we will give you information about Promise.all() and map() with Async/Await by Example. Hear we will give you detail about Promise.all() and map() with Async/Await by ExampleAnd how to use it also give you demo for it if it is necessary.
In this quick example, we’ll learn how to use Promise.all()
and map()
with Async/Await
in JavaScript to impelemt certain scenarios that can’t be implemented with for loops with async/await
.
Example of JavaScript Promises that Rely on Each Other
More often than not when making asynchronous operations, in real-world apps. such as http requests that return some data from a server, you would need to make other subsequent requests that rely to each other.
For example, let’s presume, we have the following method for getting a bunch of product categories
// First promise returns an array after a delayjsconst getProducts = () => { return new Promise((resolve, reject) => { return setTimeout( () => resolve([{ id: 'product1' }, { id: 'product2' }, { id: 'product3'}, { id: 'product4'}]), 1000 ) })}
We use the setTimeout
method to resolve the promise with an array of products after one second to simulate a real http request which would normally take some time to return.
After fetching the products, we’ll need to get the ID of each product with code similar to this:
// this promise depends on the result of the previous promiseconstgetProductId=(category)=>{returnnewPromise((resolve,reject)=>{returnsetTimeout(()=>resolve(category.id),1000)})}
We’ll also need a third promise that depends on the second promise:
constcapitalizeId=(id)=>{returnnewPromise((resolve,reject)=>{returnsetTimeout(()=>resolve(id.toUpperCase()),700)})}
Combining And Resolving all Promises with For Loop and Async/Await
Now, how to combine all these promises to get the products and capitalize their IDs?
Since the first, returns an array, we can wait for it to resolve and iterate over the array and run the other two promises subsequently i.e for each array item, we wait for the getProductId
promise to be resolved then call the capitalizeId
promise as follows:
constcapitalizeProductsIds=async()=>{constproducts=awaitgetProducts()for(letproductofproducts){constproductId=awaitgetProductId(product);console.log(productId);constcapitalizedId=awaitcapitalizeId(productId);console.log(capitalizedId);}console.log(products);}capitalizeProductsIds()
Combining the async/await
syntax with the for..of
loop will give us the following output
product1PRODUCT1product2PRODUCT2product3PRODUCT3product4PRODUCT4(4) [{…}, {…}, {…}, {…}]
That’s not the behavior that we are really looking to implement but instead we want to execute the first promise and wait for it to complete, then the second promise and finally the third promise when the second is fully completed.
Combining And Resolving all Promises with Promise.all()
, map()
and Async/Await
So instead of using the for loop with the async/await
syntax, we need to use the Promise.all()
and map()
methods with async/await
as follows:
constcapitalizeProductsIds=async()=>{constproducts=awaitgetProducts()Promise.all(products.map(async(product)=>{constproductId=awaitgetProductId(product);console.log(productId);constcapitalizedId=awaitcapitalizeId(productId)console.log(capitalizedId);}))console.log(products);}capitalizeProductsIds();
This is the output:
(4) [{…}, {…}, {…}, {…}]product1product2product3product4PRODUCT1PRODUCT2PRODUCT3PRODUCT4
So the first promise will be resolved then all the promises returned from the getProductId()
for each product and then finally all the promises returned from capitalizeId()
for each ID returned from the previous promises.
According to MDN:
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input’s promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.
Hope this code and post will helped you for implement Promise.all() and map() with Async/Await by Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs