Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple asynchronous functions then execute callbacks?

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?

like image 256
CaribouCode Avatar asked Sep 28 '15 17:09

CaribouCode


People also ask

Can callbacks be asynchronous?

Callbacks are not asynchronous by nature, but can be used for asynchronous purposes. In this code, you define a function fn , define a function higherOrderFunction that takes a function callback as an argument, and pass fn as a callback to higherOrderFunction .

Can two async functions run at the same time?

You can call multiple asynchronous functions without awaiting them. This will execute them in parallel. While doing so, save the returned promises in variables, and await them at some point either individually or using Promise. all() and process the results.

How do I make multiple calls from async?

In order to run multiple async/await calls in parallel, all we need to do is add the calls to an array, and then pass that array as an argument to Promise. all() . Promise. all() will wait for all the provided async calls to be resolved before it carries on(see Conclusion for caveat).

Are callbacks synchronous or asynchronous?

There are 2 kinds of callback functions: synchronous and asynchronous. The synchronous callbacks are executed at the same time as the higher-order function that uses the callback. Synchronous callbacks are blocking.


2 Answers

Promises give you Promise.all() (this is true for native promises as well as library ones like bluebird's).

Update: Since Node 8, you can use util.promisify() like you would with Bluebird's .promisify()

var requestAsync = util.promisify(request); // const util = require('util') var urls = ['url1', 'url2']; Promise.all(urls.map(requestAsync)).then(allData => {     // All data available here in the order of the elements in the array }); 

So what you can do (native):

function requestAsync(url) {     return new Promise(function(resolve, reject) {         request(url, function(err, res, body) {             if (err) { return reject(err); }             return resolve([res, body]);         });     }); } Promise.all([requestAsync('url1'), requestAsync('url2')])     .then(function(allData) {         // All data available here in the order it was called.     }); 

If you have bluebird, this is even simpler:

var requestAsync = Promise.promisify(request); var urls = ['url1', 'url2']; Promise.all(urls.map(requestAsync)).then(allData => {     // All data available here in the order of the elements in the array }); 
like image 51
Madara's Ghost Avatar answered Sep 20 '22 22:09

Madara's Ghost


Sounds like async.parallel() would also do the job if you'd like to use async:

var async = require('async');  async.parallel({     one: function(parallelCb) {         request('http://www.example1.com', function (err, res, body) {             parallelCb(null, {err: err, res: res, body: body});         });     },     two: function(parallelCb) {         request('http://www.example2.com', function (err, res, body) {             parallelCb(null, {err: err, res: res, body: body});         });     },     three: function(parallelCb) {         request('http://www.example3.com', function (err, res, body) {             parallelCb(null, {err: err, res: res, body: body});         });     } }, function(err, results) {     // results will have the results of all 3     console.log(results.one);     console.log(results.two);     console.log(results.three); }); 
like image 23
Ben Avatar answered Sep 20 '22 22:09

Ben