Call Multiple Api Urls And Call At Same Time
I have three API urls, each have the same object names, I wish to call all apis at the same time. My js so far: $(document).ready(function() { var first = 'https:first';
Solution 1:
API calls are asynchronous, and they execute in the sequence you write them inside your code. execution doesn't really matter because the 'then' could be called in different order as it was executed.
If you want to do anything on the execution of all three services i would recommend using async.js. look at the following example:
links = ['http://first','http://second','http://third']
data = [];
$('#get-data').click(function() {
// ...async.each(links, function(link,callback){
$.getJSON(link, function(res){
data.push(res);
callback();
})
}, function(err){
if(!err){
// your code goes here// data[0] contains data from link 1// data[1] contains data from link 1// data[2] contains data from link 2
}
})
// ...
});
Post a Comment for "Call Multiple Api Urls And Call At Same Time"