Run function asynchronously in Google Apps Script

空扰寡人 提交于 2020-07-22 06:01:44

问题


I'm making a Slack bot that calls a GAS function. Everything is working except Slack shows an error message because it only waits 3 seconds for a response when calling an API.

Can anyone help me to work out how to run everyDay2 asynchronously so that I can return the response before it's finished. I've tried Promises and callbacks but I can't solve it.

function doPost(e){

  const promise = new Promise(everyDay2);

  return ContentService.createTextOutput('thinking...');

}

回答1:


Promises doesn't work. Use triggers instead:

function doPost(e) {
  runAfter1s('everyDay2');
  return ContentService.createTextOutput('thinking...🧘');
}

const runAfter1s = func =>
  ScriptApp.newTrigger(func)
    .timeBased()
    .after(1000)
    .create();

Make sure to delete the created trigger inside everyDay2 after being triggered.



来源:https://stackoverflow.com/questions/62806366/run-function-asynchronously-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!