0

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?

Community
  • 1
  • 1
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • Can you include `loadGames` and `loadPlayoffs` functions at Question? – guest271314 Dec 29 '16 at 09:20
  • What is the point of `$.Deferred(loadGames)`? Does `loadGames()` explicitly resolve the deferred? Do you realize that deferreds have no magical powers to detect the completion of async operations and don't resolve themselves. Someone has to call `.resolve()` on the deferred itself. – jfriend00 Dec 29 '16 at 09:20
  • I suspect you'll have to show us the code for both `loadGames()` and `loadPlayoffs()` for us to help you know when they are actually done. Wrapping them in a deferred won't do that for you. – jfriend00 Dec 29 '16 at 09:21
  • @jfriend00 I am no expert on this, I just saw that in order to have a variable of array of functions and be able to use them in `$.when` you needed to use `$.Deferred`. I just want to be able to do as I used to: `$.when(loadGames(), loadPlayoffs()).done(loadPostGamesLoadData())` but with a different and variable number of functions. – dabadaba Dec 29 '16 at 09:22
  • To use `$.when()` like you showed, `loadGames()` and `loadPlayoffs()` has to return a promise or deferred. What do they return? We need to see the code in those functions to see your whole picture here. – jfriend00 Dec 29 '16 at 09:23
  • ok I am clearly doing it wrong then, can you let me know what should be returned instead? I don't understand why it would work when an array is not used though – dabadaba Dec 29 '16 at 09:24
  • both `loadGames` and `loadPlayoffs` are returning `$.ajax({...})`, that's the promise we need right? – dabadaba Dec 29 '16 at 09:30
  • If both `loadGames` and `loadPlayoffs` return jQuery promises `$.Deferred()` is not necessary. Call the functions when passing as parameter to `.push()`. – guest271314 Dec 29 '16 at 09:43
  • This question is hopelessly misleading and incomplete and offers no useful value going forward because of that. It would have been much better if you edited your question (you can use the "edit" link to clarify your question to show us what is really going on here). Then, you could have showed people what `loadGames()` and `loadPlayoffs()` are returning and we could have offered a proper answer here. As it is, the accepted answer does not work for you at all as your code is currently written so this is a misleading question and answer. – jfriend00 Dec 29 '16 at 09:54
  • This is basically a dup of http://stackoverflow.com/questions/41376908/when-with-array-of-deferreds-not-calling-done-action which has more complete information on the actual problem. – jfriend00 Dec 29 '16 at 09:57

2 Answers2

4

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.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks... it's early in the morning. What do you mean with that last piece though? Because now it doesn't seem like the `done` function is being called at all. – dabadaba Dec 29 '16 at 09:18
  • I suspect this is only part of the problem. I rather doubt that `loadGames` and `loadPlayoffs` are resolving the deferreds. – jfriend00 Dec 29 '16 at 09:22
  • as I said in another comment, those functions return `$.ajax({...})`, isn't this resolving the deferreds? – dabadaba Dec 29 '16 at 09:31
  • 1
    If they **return** `$.ajax` you don't need `$.Deferred`, just do `deferredLoads.push(loadGames());` etc – adeneo Dec 29 '16 at 09:40
1

remove the () in the done callback:

.done(loadPostGamesLoadData);

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

Jai
  • 74,255
  • 12
  • 74
  • 103