Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous request in Node.js

If I need to call 3 http API in sequential order, what would be a better alternative to the following code:

http.get({ host: 'www.example.com', path: '/api_1.php' }, function(res) {    res.on('data', function(d) {       http.get({ host: 'www.example.com', path: '/api_2.php' }, function(res) {        res.on('data', function(d) {           http.get({ host: 'www.example.com', path: '/api_3.php' }, function(res) {            res.on('data', function(d) {              });         });         }       });     });     }   }); }); } 
like image 766
Howard Avatar asked May 18 '11 17:05

Howard


People also ask

What is synchronous in NodeJS?

Synchronous methods: Synchronous functions block the execution of the program until the file operation is performed. These functions are also called blocking functions. The synchronous methods have File Descriptor as the last argument.

What is a synchronous request?

With synchronous requests, your client application sends a request to NetSuite, and the client waits until the request is processed and a response is returned. That is, the application does not proceed with other work until receiving the response.

Does NodeJS support synchronous?

js docs puts it, blocking is when the execution of additional JavaScript in the Node. js process must wait until a non-JavaScript operation completes. Blocking methods execute synchronously while non-blocking methods execute asynchronously.


2 Answers

Using deferreds like Futures.

var sequence = Futures.sequence();  sequence   .then(function(next) {      http.get({}, next);   })   .then(function(next, res) {      res.on("data", next);   })   .then(function(next, d) {      http.get({}, next);   })   .then(function(next, res) {     ...   }) 

If you need to pass scope along then just do something like this

  .then(function(next, d) {     http.get({}, function(res) {       next(res, d);     });   })   .then(function(next, res, d) { })     ...   }) 
like image 186
Raynos Avatar answered Sep 23 '22 08:09

Raynos


I like Raynos' solution as well, but I prefer a different flow control library.

https://github.com/caolan/async

Depending on whether you need the results in each subsequent function, I'd either use series, parallel, or waterfall.

Series when they have to be serially executed, but you don't necessarily need the results in each subsequent function call.

Parallel if they can be executed in parallel, you don't need the results from each during each parallel function, and you need a callback when all have completed.

Waterfall if you want to morph the results in each function and pass to the next

endpoints =   [{ host: 'www.example.com', path: '/api_1.php' },   { host: 'www.example.com', path: '/api_2.php' },   { host: 'www.example.com', path: '/api_3.php' }];  async.mapSeries(endpoints, http.get, function(results){     // Array of results }); 
like image 44
Josh Avatar answered Sep 21 '22 08:09

Josh