0

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 );
5
  • I think you can't do that, since the $.ajax call waiting the response from the server so you have no return value while you call the return before the server give the response. Commented Jul 13, 2016 at 4:09
  • @FadhlyPermata - You can just return the promise from the function which is the same thing that $.ajax() returns. Commented Jul 13, 2016 at 4:38
  • Your naming of the callbacks goodFetches, badFetches suggests you don't exactly understand what $.when does. It's more like .then(allGood, anyBad) (and just like with any promise, only one of the two will get called). Commented Jul 13, 2016 at 5:50
  • The behaviour of $.when when being passed ajax promises is indeed abysimal. Switch to a proper promise library if you can. Commented Jul 13, 2016 at 5:54
  • Your snippets with and without the ajax calls wrapped in functions are exactly equivalent. $.when does not behave differently. I'd suggest you show us what those goodFetches and badFetches callbacks are and what goes wrong in them (and what you would have expected instead) Commented Jul 13, 2016 at 5:56

1 Answer 1

2

It will work just fine in the functions. You are probably just not accessing the arguments properly from the result. You get a series of arguments that are each a sub array that contains three elements: [data, statusText, jqXHR]. Those arguments will be the same whether you have your ajax calls in a function or not as long as the function returns the promise.

Here's an example from the jQuery doc:

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
  // a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
  // Each argument is an array with the following structure: [ data, statusText, jqXHR ]
  var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
  if ( /Whip It/.test( data ) ) {
    alert( "We got what we came for!" );
  }
});

It makes no difference if the $.ajax() calls are directly in the $.when() arguments or if you return the promise from a function. Same result.


So, you can do this and you will get the same data back in the .then() handler:

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');
};

$.when( myAjaxCall1(), myAjaxCall2() ).then(function(a1, a2) {
      // a1[0] is data from first ajax call
      // a2[0] is data from second ajax call
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.