The jQuery doco shows an example where two $.ajax calls are made under the control of a $.when (page down the 'Examples' heading) .
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
That's fine but what I would like to do is to define those $.ajax calls within a function (where I might do other stuff to prepare for the call).
So something like this :
$.when( myAjaxCall1(), myAjaxCall2() )
.then( goodFetches, badFetches );
I have tried that with a functions that look like this :
var myAjaxCall1 = function()
{
return $.ajax('https://httpbin.org/get?Z=Y,X,W');
};
var myAjaxCall2 = function()
{
return $.ajax('https://httpbin.org/get?A=B,C,D');
};
I do get a three element array as an argument to goodFetches for each of myAjaxCall1 and myAjaxCall2 but it's not the same information as I receive if I just put the $.ajax calls straight into the $.when as shown in the above example from the documentation.
How should I restructure myAjaxCall1 and myAjaxCall2 so that the behaviour is equivalent to :
$.when( $.ajax( "https://httpbin.org/get?Z=Y,X,W" ),
$.ajax( "https://httpbin.org/get?A=B,C,D" ) )
.then( goodFetches, badFetches );
returnbefore the server give the response.$.ajax()returns.goodFetches, badFetchessuggests you don't exactly understand what$.whendoes. It's more like.then(allGood, anyBad)(and just like with any promise, only one of the two will get called).$.whenwhen being passed ajax promises is indeed abysimal. Switch to a proper promise library if you can.$.whendoes not behave differently. I'd suggest you show us what thosegoodFetchesandbadFetchescallbacks are and what goes wrong in them (and what you would have expected instead)