dabadaba
dabadaba

Reputation: 9522

$.when() array of deferreds

I need to pass a variable amount of deferred functions to $.when so they need to be packed in an array. I have tried what this and this suggests, but the done action is not being performed after all the deferred functions are done. Not even when there's just one in the array.

This is my actual try:

function loadAllGames(update_games, update_playoffs) {
    var deferredLoads = [];
    if (update_games !== false)
        deferredLoads.push($.Deferred(loadGames));

    if (update_playoffs !== false)
        deferredLoads.push($.Deferred(loadPlayoffs));

    $.when.apply($, deferredLoads).done(loadPostGamesLoadData());
}

I am console logging something in loadPostGamesLoadData and I can see what's being logged way before the games have been loaded.

So what's the actual way to do this? How can I have a variable set of deferred functions be called and then perform an action when all of them are done?

Upvotes: 0

Views: 54

Answers (2)

adeneo
adeneo

Reputation: 318212

You're calling loadPostGamesLoadData, not referencing it

$.when.apply($, deferredLoads).done(loadPostGamesLoadData);

Otherwise it looks fine, assuming loadGames and loadPlayoffs are functions that resolve or reject the Deferred.

Upvotes: 4

Jai
Jai

Reputation: 74738

remove the () in the done callback:

.done(loadPostGamesLoadData);

Your problem is that you are calling the method instead of having a reference of it.

Upvotes: 1

Related Questions