jQuery.when - multiple Deferred objects passed #262

Closed
fastfasterfastest opened this Issue Mar 4, 2013 · 4 comments

Projects

None yet

5 participants

@fastfasterfastest

http://api.jquery.com/jQuery.when/ states: 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.

However, I think the last sentence is incorrect. I believe the resolved values of a jQuery.ajax() request is NOT a jqXHR object as that sentence states, but rather three values - data, textStatus and the jqXHR object.

Thus, the last sentence should be changed to something along the lines: For example, when the Deferreds are jQuery.ajax() requests, the arguments will be an array for each of the requests (each array containing data, textStatus and the jqXHR object of the request), in the order they were given in the argument list.

@dmethvin
Member

I think you're confusing the return value of a jQuery.ajax() call, which is a jQXHR, with the arguments passed to the .done() or .fail() methods when an ajax request completes. The current documentation looks correct to me.

@dmethvin dmethvin closed this Mar 13, 2013
@fastfasterfastest

No, I am not confused - I know very well what $.ajax returns and what gets passed to .done when the promise is resolved. As my second paragraph states, the resolved values of a $.ajax request are data, textStatus and the jqXHR object.

Let me try it with an example: http://jsfiddle.net/fastfasterfastest/JxNRW/

$.when(
    $.ajax({
        type: "POST",
        url: "/echo/json/",
        data: { json: '"request 1"' }
    }),
    $.ajax({
        type: "POST",
        url: "/echo/json/",
        data: { json: '"request 2"' }
    })
)
.done(function(arg1, arg2){
    alert("ajax request 1: textStatus=" + arg1[1] + ", data=" + arg1[0] + "\n"+
          "ajax request 2: textStatus=" + arg2[1] + ", data=" + arg2[0] + "\n");

});

The documentation for $.when claims that arg1 and arg2 will be the jqXHR objects for the ajax requests. That is incorrect.

Instead, arg1 and arg2 will be the resolved values for the ajax requests, respectively. And since the resolved values for $.ajax is NOT jqXHR, but rather data, textStatus and the jqXHR object, that is what arg1 and arg2 will be.

So, I believe the documentation is incorrect and I urge you to re-open the issue.

@dmethvin
Member

You're right, I wrote the original docs and keep thinking about it the wrong way, although I've used $.when a few times.

@dmethvin dmethvin reopened this Mar 15, 2013
@AurelioDeRosa
Member

I think this issue has been addressed by PR #545 so it can be closed.

@arthurvr arthurvr closed this Jan 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment