Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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) });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!