collect data

56 views
Skip to first unread message

Tomas Garijo

unread,
Jun 2, 2015, 2:51:04 PM6/2/15
to nod...@googlegroups.com
Hello. 
I need to call many servers with xml documents. Some servers can last more time to response and others less. After some time I have collect   all responses and  make a xml document. What mechanism I have to use to implement this?

Regards.



Tim Davis

unread,
Jun 3, 2015, 9:46:17 AM6/3/15
to nod...@googlegroups.com
Hi. Here is a simple example using promises.


'use strict';

// require the `request` module
var request = require('request');

// `gets` will hold an array of requests to send
var gets = [];

// here are the APIs you want to hit
var endpoints = [
];

// build up the `gets` array
endpoints.forEach(function (url) {

  var req = new Promise(function (resolve, reject) {

    // this is a simple GET with the `request` module
    return request(url, function (err, res, body) {
      // success: resolve the body content
      if (!err && res.statusCode === 200) {
        return resolve(body);
      }
      else {
        // reject with a useful error
        reject(YouErrorHere);
      }
    });

  });

  gets.push(req);

});

// send all requests and wait until they have all succeeded or one give an error
return Promise.all(gets)
  .then(function (responseObjects) {
    // do something with array of response objects
  });

Tim Davis

unread,
Jun 3, 2015, 9:46:29 AM6/3/15
to nod...@googlegroups.com
You can use the "async" package or Promise.all to call your processResponses function when all requests have completed.

Tomas Garijo

unread,
Jun 5, 2015, 11:22:23 AM6/5/15
to nod...@googlegroups.com
Ok Thank You.

Regards
Reply all
Reply to author
Forward
0 new messages