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

175
Visualizações
Need Functions That Wait Until Cache Can Be Verified

I'm aware that the immediate response is going to be to use promises for the following problem. However, I've looked at every example I can and nothing seems to be readable to me. Most is done through HTML examples, which I don't understand. Similar posts here have a plethora of arguments on which syntax is appropriate which furthers the confusion. So, please show me how I might use a promise to do the following or if another solution is better.

I'd like for a function to check if the cache is available, and if not, it will reinitialize the cache:

function valData(){
  if (arrOfNames || arrOfRanges == null){
    initializeGame();
    Logger.log('Game Initialized')
  }
  else {
    Logger.log('Data Available')
  }
};

I'm aware this is likely sloppy, but stay with me

This function works as intended, as does the following:

function reqVal(valname,x){
  if (x == 0){
    var nameArr = convToString(arrOfNames);
    var valIndex = nameArr.indexOf("" + valname)
    return arrOfRanges[valIndex];
  }
  else {
    var nameArr = convToString(arrOfNames);
    var valIndex = nameArr.indexOf("" + valname)
    return valIndex;
  }
};

Again, very very sloppy but Ill clean things up once I can make them function

Both of these scripts work fine on their own and return what I need. But if I have a third script:

function test(){
  valData();
  var valNum = reqVal('curHP', 0);
  var valIndex = reqVal('curHP',1)
  var newNum = valNum - 5;
  Logger.log(valNum)
  Logger.log(newNum)
  Logger.log(valIndex)
};

The reqVal() function fails due to the asynchronous nature of GAS and the Cache having expired, but the valData() function succeeds and produces the needed array. So, I know I need to add a promise in valData() that the reqVal() function can read from but have no idea how to format that as examples I've seen have been very unclear.

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