1

Is there a place I can find more information about jquery's when function? Basically, what I do is (pseudo-code)

$.when(ajaxCall1, ajaxCall2)).done(function(data1, data2) {
console.log(data1);
console.log(data2);
// do something real with the data
});

so what I don't get is, in my ajaxCall1, I used to do something like this when it was a standalone function and not in jquery's when():

    $.ajax({
        url: '/api/platform/' + platform,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            console.log("Got data");
            self.platforms = data;
            self.eventAggregator.trigger('getplatforms');  
        },
    });

So the data parameter in that function looks different than what the $.when() returns. It looks like $.when returns an array by logging it. So I'm blindly going

self.platforms = data[0];

So is there more documentation on the subject somewhere? I don't feel comfortable blindly getting the first parameter of the array without knowing what it is, and I don't know what to do if there is an error in the call.

My ajaxCall1 methods basically just return the $.ajax call.

i.e.

ajaxCall1() {
return $.ajax....
}
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • 1
    Did you read http://api.jquery.com/jQuery.when/? – bfavaretto Jun 19 '13 at 02:15
  • @bfavaretto I did, but looking at that one page of documentation, it doesn't really have much unless I'm missing something. – Crystal Jun 19 '13 at 02:19
  • Does your actual code actually have `$.when(ajaxCall1, ajaxCall2))`? It's a syntax error (extra closing parentheses at the end). – bfavaretto Jun 19 '13 at 02:25
  • @bfavaretto No it doesn't. That was just pseudo code. I guess I'm trying to find more info on the $.when. Like why do my data1 and data2 objects get wrapped in an array for each of them? When I do a normal ajax request, they do not. So I'm trying to find out those things that don't seem to be in the docs for me. And like I said in my question, how to do deal with errors. ARe those in the data1 and data2 objects? – Crystal Jun 19 '13 at 02:36
  • 1
    When any of the requests fail, it invokes the fail callback. So the basic pattern is `$.when(ajaxCall1, ajaxCall2)).done(function(data1, data2){}).fail(function(/* jqXHR, I think */){});` If you are unsure of what data1 and data2 are, did you console.log them? – bfavaretto Jun 19 '13 at 02:45
  • 1
    @bfavaretto Ok that makes sense about the fail. But the data part is still funny to me. Cause when I call my ajax1 on its own, my data returned to me is not in an []. But when I do the .when, it gets returned in []. So not sure what's up with that. – Crystal Jun 19 '13 at 02:47
  • I reverted my answer to the original quote I had posted. You're right, the documentation is not clear about what's going on. – bfavaretto Jun 19 '13 at 03:08

2 Answers2

1

You're right, the documentation is not clear about that. It says (emphasis mine):

In the case where multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed. The method will resolve its master Deferred as soon as all the Deferreds resolve, or reject the master Deferred as soon as one of the Deferreds is rejected. If the master Deferred is resolved, it is passed the resolved values of all the Deferreds that were passed to jQuery.when. For example, when the Deferreds are jQuery.ajax() requests, the arguments will be the jqXHR objects for the requests, in the order they were given in the argument list.

It's wrong that the arguments will be the jqXHR objects. The done method is actually passed arrays containing data, status, jqXHR for each request.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1

$.when does two very different things based on how many promise objects are passed in.

If you only pass in one promise object, it will act exactly the same as the success callback you are used to.

Otherwise, if you pass in 2 or more promise objects, each parameter will be an array containing the three arguments that are normally passed to the success callback. To get what you're used to, access the first item in said array.

console.log(data1[0])
console.log(data2[0])

Note, this assumes the promise objects are jQXHR objects. Otherwise, the array will contain the parameters passed to the deferred object's resolve method.

http://jsfiddle.net/LSpcK/

Kevin B
  • 94,570
  • 16
  • 163
  • 180