javascript - Jquery deferred from callback of ajax calls -


i'm trying write procedure after 2 objects returned result of callback of ajax function.

i know classic example of using jquery when():

$.when($.get("http://localhost:3000/url1"),  $.get("http://localhost:3000/url2").done(//do something)); 

but in case, don't want trigger when on execution of ajax function, want when trigger callback execution of ajax function. like:

$.get("http://localhost:3000/url1", function(data){   function(){     //do data, , return myobj1;   } });  $.get("http://localhost:3000/url2", function(data){   function(){     //do data, , return myobj2;   } });  $.when(obj1, obj2).done(function(){   //do these 2 objects }); 

but of course, doesn't work. ideas?

that should work. jquery.when() takes multiple arguments , fires once have completed returning each results arguments array:

var req1 = $.get("http://localhost:3000/url1"); var req2 = $.get("http://localhost:3000/url2");  $.when(req1, req2).done(function(res1, res2) {     //do these 2 objects }); 

if don't want handle requests can create own deferreds , use those:

var deferred1 = $.deferred(),     deferred2 = $.deferred();  $.get("http://localhost:3000/url1", function(data){     function(){         //do data, , return myobj1;         deferred1.resolve(myobj1);     } });  $.get("http://localhost:3000/url2", function(data){     function(){         //do data, , return myobj2;         deferred2.resolve(myobj2);     } });  $.when(deferred1, deferred2).done(function(){     //do these 2 objects }); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -