How to make multiple requests in a loop with async/await
I have come across, where people find some difficulty to make multiple requests when using async/await.
Async/await is introduced to overcome then/catch chaining issue which is also called snake code.
I have added a sample repo with basic nodejs server client code with one GET and POST REST call.
Server.js
I have used express module . It has two Rest calls.
app.get(‘/api/getData’, modelRoute.getData);
app.post(‘/api/postData’, modelRoute.postData);
Get Ajay kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
As you can see in server.js
Model.js
model.js has a handler for each rest call as you can see. res.send will be sent from here.
getData returns
{status: "ok",data:[1,2,3,4,5]}
postData returns
{status: `ok for id= ${id}`,}
server log and result
Client.js
I have used Axios module to make rest calls.
client.js has one method called post_req_in_loop which is an async/await function.
First, it makes a GET call to get all the ids then it makes POST call in the loop using await and try/catch. Once the loop is completed, results will have all the post results added then only the below code will be executed.
console.log("now send results by res.send")client log and results
You can see in the screenshots, how it is executed.
Hope it helps!.
The code is available at GitHub. Follow me to get more technical topic blogs.
Please, put your comment if you found this article useful. You can also leave a comment on the next problem or any topic you want to learn.





