DEV Community

Discussion on: for loop vs .map() for making multiple API calls

Collapse
 
rajeshmoka22 profile image
Rajesh Moka

This is a great article. I had trouble understanding the last example without async await but with promises. I knew promise.all takes promises array as an argument, but why did we write promise in each fetch call? Can u explain a little

Collapse
 
askrishnapravin profile image
Krishna Pravin

The last example(using promise) is the same as the previous one(using await).

We have a promise inside fetch because parsing response as json response.json() returns a promise. For each API call, Promise.all() will first wait for the API call's response to arrive, and then it will wait for the json parsing to complete.

When Promise.all takes an array of promises, it will wait for all the inner promises as well to get resolved.