【问题标题】:How to run a function after a GAS function has really ended?GAS 函数真正结束后如何运行函数?
【发布时间】:2019-10-17 13:21:54
【问题描述】:

如何在 myFunc 真正结束后让这个 otherFunc() 运行?

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");
}

这是 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();
}
}

这是 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;
        }   
     }
     });
  }
}

使用 promise 并没有让 otherFunc() 在 myFunc() 之后运行,我仍然需要使用 setTimeOut,这对这种情况不利。

【问题讨论】:

  • 我认为在你的脚本中,当func()运行时,serverFunc()myFunc(e)otherFunc()按顺序运行。例如,您可以将myFunc() 的脚本添加到您的问题中吗?我想确认一下您的情况。
  • 我做了一些修改
  • 您需要在所有google.script.run 调用中使用async-await。执行顺序:serverFunc()>myFunc()>otherFunc()。但是,myFunc() 执行在 function(nama) 执行之前完成。
  • 感谢您添加信息。关于myFunc(),请像@TheMaster 的评论一样修改func()。作为其他观点,我认为myFunc() 是不完整的。因为我认为由于语法错误,google.script.run.withSuccessHandler(function(nama){dataArray.forEach(function(r){justCreatingTableRows();}).listNamaBarang(); 发生了错误。 dataArray 是在其他地方声明的吗?请再次确认。

标签: javascript google-apps-script promise async-await


【解决方案1】:

当你的脚本被修改时,这个修改怎么样?请认为这只是几个答案之一。

修改点:

  • 在您的脚本中,google.script.run 也用于myFunc() 的函数中。 google.script.run 与异步进程一起运行。这样,当func() 运行时,serverFunc()myFunc(e)otherFunc() 按顺序运行,而google.script.run.withSuccessHandler(function(nama){ })).listNamaBarang()function(nama) 则按此顺序运行。这样,您的问题中的问题就出现了。 TheMaster's comment 已经提到了这一点。
    • 为了避免这种情况,请将myFunc()修改为func()

修改后的脚本:

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()
}
  • 在这种情况下,我认为其他功能不需要修改。
  • 此修改假设声明了dataArray

参考:

  • Class google.script.run
    • google.script.run 是 HTML 服务页面中可用的异步客户端 JavaScript API,可以调用服务器端应用脚本函数。

【讨论】:

  • 这个修改很完美。 arigato gozaimasu ^_^
  • @Makoto Daiwa Ambara 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多