Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
152 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...