Search code examples
javascriptjqueryjquery-deferred

Function returning before jQuery promise within promise is resolved


I have a function which calls dealCardSelectableAI(), which sets up a number of jQuery deferred promises. The function setCardName() is then called from within it. Once both functions complete their tasks saveGame() should then be triggered.

Everything works, except setCardName() does not complete before saveGame() is triggered. It appears that deferredQueue.push(setCardName(system, result)); is not operating as I expected. I am not sure where I'm going wrong or how to resolve the issue.

var setCardName = function (system, card) {
  var deferred = $.Deferred();

  require(["cards/" + card[0].id], function (data) {
    var cardName = loc(data.summarize());
    system.star.ai().cardName = ko.observable(cardName);
    deferred.resolve();
  });
  return deferred.promise();
};

var dealCardSelectableAI = function (win, turnState) {
  var deferred = $.Deferred();

  // Avoid running twice after winning a fight
  if (!win || turnState === "end") {
    var deferredQueue = [];

    _.forEach(model.galaxy.systems(), function (system, starIndex) {
      if (
        model.canSelect(starIndex) &&
        system.star.ai() &&
        system.star.ai().treasurePlanet !== true
      ) {
        deferredQueue.push(
          chooseCards({
            inventory: inventory,
            count: 1,
            star: system.star,
            galaxy: game.galaxy(),
            addSlot: false,
          }).then(function (result) {
            deferredQueue.push(setCardName(system, result));
            system.star.cardList(result);
          })
        );
      }
    });

    $.when(deferredQueue).then(function () {
      deferred.resolve();
    });
  } else {
    deferred.resolve();
  }

  return deferred.promise();
};

dealCardSelectableAI(false).then(saveGame(game, true));

Solution

  • Your code says call saveGame() and what is returned from the function call should be set to then. It is not saying, "call saveGame when done"

    dealCardSelectableAI(false).then(function () { saveGame(game, true) });