Why does deferred.when () return promises in the executed callback?

Why is jQuery deferred.when

returning promises in the callback done

and not their corresponding response data?

var data = {
    json: JSON.stringify({
        text: 'some text',
        array: [1, 2, 'three'],
        object: {
            par1: 'another text',
            par2: [3, 2, 'one'],
            par3: {}
        }
    }),
    delay: 3
};

var firstRequest =  $.ajax({
    url:'/echo/json/',
    data: data,
    type: 'POST'
});

var secondRequest =  $.ajax({
    url:'/echo/json/',
    data: data,
    type: 'POST'
});

$.when.apply($, [firstRequest, secondRequest]).done(function(data1, data2){
    console.log(data1); // returns array, I expect a response object
    console.log(data2); 
});

      

Both the documentation and the various answers on SO, for example, imply that I should be getting the actual response objects, not the arrays with [responseobject, textstatus, jqxhr]

.

http://jsfiddle.net/2h48mr78/

+3


source to share


1 answer


You return the correct structure, but the arguments data1

and data2

are not actual data. It's actually an array with three elements, data, statusText, jqXHR

so your data is actually in data1[0]

that logout:

Object {text: "some text", array: Array[3], object: Object}

      



The documentation is a bit obscure, but shows how it works in the Example section.

+4


source







All Articles