Synchronous request in nodejs

javascript – Synchronous request in nodejs


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.series(endpoints, http.get,function(results){
    // Array of results
});

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){
    ...
  })
varAPIs=['/api_1.php','/api_2.php','/api_3.php'];
var host ='www.example.com';

function callAPIs ( host,APIs){
  var API =APIs.shift();
  http.get({ host: host, path: API },function(res){ 
    var body ='';
    res.on('data',function(d){
      body += d; 
    });
    res.on('end',function(){
      if(APIs.length ){
        callAPIs ( host,APIs);
      }
    });
  });
}

callAPIs( host,APIs);

 Common Node library:

functionget(url){
  returnnew(require('httpclient').HttpClient)({
    method:'GET',
      url: url
    }).finish().body.read().decodeToString();
}

var a =get('www.example.com/api_1.php'), 
    b =get('www.example.com/api_2.php'),
    c =get('www.example.com/api_3.php');

Using the request library can help minimize the cruft:

var request = require('request')

request({ uri:'http://api.com/1'},function(err, response, body){
    // use body
    request({ uri:'http://api.com/2'},function(err, response, body){
        // use body
        request({ uri:'http://api.com/3'},function(err, response, body){
            // use body
        })
    })
})
var request = require('request')
varStep    = require('step')

// request returns body as 3rd argument
// we have to move it so it works with Step 😦
request.getBody =function(o, cb){
    request(o,function(err, resp, body){
        cb(err, body)
    })
}

Step(
    function getData(){
        request.getBody({ uri:'http://api.com/?method=1'},this.parallel())
        request.getBody({ uri:'http://api.com/?method=2'},this.parallel())
        request.getBody({ uri:'http://api.com/?method=3'},this.parallel())
    },
    function doStuff(err, r1, r2, r3){
        console.log(r1,r2,r3)
    }
)

 

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s