In my Node.js code I need to make 2 or 3 API calls, and each will return some data. After all API calls are complete, I want to collect all the data into a single JSON object to send to the frontend.
I know how to do this using the API callbacks (the next call will happen in the previous call's callback) but this would be slow:
//1st request
request('http://www.example.com', function (err1, res1, body) {
//2nd request
request('http://www.example2.com', function (err2, res2, body2) {
//combine data and do something with it
});
});
I know you could also do something similar and neater with promises, but I think the same concept applies where the next call won't execute until the current one has finished.
Is there a way to call all functions at the same time, but for my final block of code to wait for all API calls to complete and supply data before executing?
See Question&Answers more detail:os