1

How to make this otherFunc() run after myFunc has really ended?

async function func() {
  const res = await (() => new Promise(r => google.script.run.withSuccessHandler((e) => r(myFunc(e))).serverFunc()))();
  console.log(res);
  otherFunc();          //this function still executed before myFunc end, except i use setTimeOut
  console.log("done");
}

This is what is inside of myFunc()

function myFunc(){
  var tbody = document.getElementById("table-permintaan");
  var thead = tbody.firstElementChild;
  while (tbody.hasChildNodes())  
    tbody.removeChild(tbody.firstChild);
  tbody.appendChild(thead);
  google.script.run.withSuccessHandler(function(nama){
   dataArray.forEach(function(r){
     justCreatingTableRows();
  }).listNamaBarang();
}
}

This is what is inside of otherFunc()

function otherFunc(){
   var btns = document.getElementsByTagName('button');
  var mdls = document.getElementsByClassName("modal_detail");
  var cds = document.getElementsByClassName("close_detail");
  for(var i = 0;i<btns.length-1;i++){
     btns[i].addEventListener("click",function(){
         document.getElementById("mdl"+this.id.slice(3)).style.display = "block";
     });
     mdls[i].style.display = "none";
  }
  for(var i=0;i<cds.length;i++){
     cds[i].addEventListener("click",function(){
        for(var j = 0;j<mdls.length;j++){
        console.log(this.id.slice(3) , mdls[j].id);
        if (this.id.slice(3) == mdls[j].id.slice(3)) {
           mdls[j].style.display = "none";
           return;
        }   
     }
     });
  }
}

Using promise didn't make the otherFunc() run after myFunc(), I still need to use setTimeOut which is not good for this case.

  • Possible duplicate of [Are there promises on Google Apps Script?](https://stackoverflow.com/questions/36561735/are-there-promises-on-google-apps-script) – Rubén Oct 17 '19 at 02:05
  • I think that in your script, when `func()` is run, `serverFunc()`, `myFunc(e)` and `otherFunc()` are run in order. For example, can you add the script of `myFunc()` to your question? I would like to confirm your situation. – Tanaike Oct 17 '19 at 02:09
  • i made some edits – Makoto Daiwa Ambara Oct 17 '19 at 02:23
  • 1
    You need to use `async-await` on all `google.script.run` calls. Order of execution: `serverFunc()` >``myFunc()``>`otherFunc()`. However, `myFunc()` execution finishes before `function(nama)` is executed. – TheMaster Oct 17 '19 at 03:04
  • Thank you for adding the information. About `myFunc()`, please modify it like `func()` as @TheMaster 's comment. As other points, I think that `myFunc()` is incomplete. Because I think that an error occurs at `google.script.run.withSuccessHandler(function(nama){dataArray.forEach(function(r){justCreatingTableRows();}).listNamaBarang();`, because of the syntax error. And is `dataArray` declared at elsewhere? Please confirm it again. – Tanaike Oct 17 '19 at 04:53
  • but it all works well if i write like this: `async function func() { const res = await (() => new Promise(r => google.script.run.withSuccessHandler((e) => r(myFunc(e))).serverFunc()))(); console.log(res); setTimeout(otherFunc,5000); console.log("done"); }` – Makoto Daiwa Ambara Oct 17 '19 at 05:18
  • I think that your replying is one of several answers. As other answer, I posted about above comment as an answer. Could you please confirm it? If that was not the result you want, I apologize. – Tanaike Oct 17 '19 at 05:21

1 Answers1

3

When your script is modified, how about this modification? Please think of this as just one of several answers.

Modification points:

  • In your script, google.script.run is also used in the function of myFunc(). google.script.run is run with the asynchronous process. By this, when func() is run, serverFunc(), myFunc(e) and otherFunc() in order, while function(nama) of google.script.run.withSuccessHandler(function(nama){ })).listNamaBarang() is run out of this order. By this, the issue in your question occurs. This has already been mentioned by TheMaster's comment.
    • In order to avoid this, please modify myFunc() like func().

Modified script:

async function myFunc() {
  var tbody = document.getElementById("table-permintaan");
  var thead = tbody.firstElementChild;
  while (tbody.hasChildNodes())  
    tbody.removeChild(tbody.firstChild);
  tbody.appendChild(thead);

  // Below script was modified.
  const res = await (() => new Promise(r => google.script.run.withSuccessHandler(function(nama) {
    dataArray.forEach(function(r){justCreatingTableRows(r)});
    return r("done at myFunc()");
  }).listNamaBarang()))();
  console.log(res) // done at myFunc()
}
  • In this case, I think that other functions are not required to be modified.
  • This modification supposes that dataArray is declared.

Reference:

  • Class google.script.run
    • google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions.

Tanaike
  • 181,128
  • 11
  • 97
  • 165