Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda