Search code examples
javascriptjquerydeferred

Returned value for $.when.apply($, $.map(array, someFunction(x){}))?


What is returned from getTickets function?

function getTickets(needsTickets) {
    return $.when.apply($, $.map(needsTickets, function(x) {
        return function(x) { return $.ajax() } ;
    }));
}

Solution

  • Let's analyze what is happening:

    The result of $.when is a promise and that is what is being returned.

    $.when doesn't accept an array as arguments so to handle an array of promises you need to do:

    $.when.apply( context, promiseArray);
    

    The promise array is being created by $.map