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

173
Views
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
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!