Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
Chaining jQuery promises

I wish to call dealCardSelectableAI(), have it chooseCards(), then use the output to set an observable system.star.cardList(), then call setCardName(). Once all this is done I want saveGame() to execute.

However, setCardName() is not completing before saveGame() is called, so apparently I can't push it into my deferredQueue via a .then().

I'm using jQuery due to working within an ES5 environment.

var setCardName = function (system, card) {
  var deferred = $.Deferred();
  require(["cards/" + card[0].id], function (data) {
    var cardName = loc(data.summarize());
    system.star.ai().cardName = 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 (card) {
            system.star.cardList(card);
            deferredQueue.push(setCardName(system, card));
          })
        );
      }
    });

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

  return deferred.promise();
};

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

I tried resolving this by changing the function calls so setCardName() was chained following dealCardSelectableAI(). However, it relies on system.star.cardList() having been written, which in some circumstances had not yet been done.

Given the dependency system.star.cardList() has on chooseCards(), I cannot figure out how to make sure it has been written to before calling setCardName() in a way which blocks saveGame() until everything is done.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda