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?