-1

I need to fetch the data from two different API endpoints, and after both data is fetched I should do something with those data (ie. compare the data from two sources).

I know how to fetch the data from one API, and then call the callback function to do something with the data. I am doing this as follows.

function getJSON(options, cb) {
   http.request(options, function(res){
       var body = "";

       res.on("data", function(chunk){
          body += chunk;
      });

       res.on("end", function(){
          var result = JSON.parse(body);
          cb(null, result);
      });

       res.on("error", cb);
   })
   .on("error", cb)
   .end();
}

var options = {
    host: "api.mydata1.org",
    port: 8080,
    path: "/data/users/3",
    method: "GET"
}

getJSON(options, function(err, result) {
    if (err) {
        return console.log("Error getting response: ", err);
    }

    // do something with the data

});

Now, what I would want to have something like:

var options2 = {
        host: "api.mydata2.org",
        port: 8080,
        path: "/otherdata/users/3",
        method: "GET"
    }

Those would be the options to connect to the other API, and have a single callback function that would get called whenever the data from both APIs is loaded. How can I do that?

wesleyy
  • 2,575
  • 9
  • 34
  • 54
  • Have a look at promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises . This topic has been covered extensively on Stack Overflow, e.g. [JavaScript Wait until all async calls finish](https://stackoverflow.com/q/34205266/218196) – Felix Kling Jul 19 '17 at 00:37

1 Answers1

1

I'd suggest using the request-promise module which returns promises that represent the async operations and you can then use Promise.all() to track when both of them are done and then access their results:

const rp = require('request-promise');

function getJSON(options) {
    return rp(options).then(body => {
        return JSON.parse(body);
    });
}

let options1 = {
    host: "api.mydata1.org",
    port: 8080,
    path: "/data/users/3",
    method: "GET"
};

let options2 = {
        host: "api.mydata2.org",
        port: 8080,
        path: "/otherdata/users/3",
        method: "GET"
};

Promise.all([getJSON(options1), getJSON(options2)]).then(results => {
    // process results here
    console.log(results[0]);      // from options1 request
    console.log(results[1]);      // from options2 request
}).catch(err => {
    // process error here
    console.log(err);
});

You can read about Promise.all() on MDN. There are lots and lots of articles and tutorials about promises in general on the web. Here's one. They are standard in ES6 Javascript and have been available for years in ES5 through libraries. They are the chosen scheme in the Javascript language for managing or coordinating asynchronous operations. If you're doing any async programming in Javascript (which pretty much all node.js programming does), then you should learn promises.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • @wesleyy - Does this answer your question? If so, you can indicate that to the community by clicking the green checkmark to the left of the answer and that will also earn you some reputation points. – jfriend00 Aug 18 '17 at 06:18