在 Google Apps 脚本中异步运行函数

Dav*_*ook 0 javascript asynchronous promise google-apps-script slack-api

我正在制作一个调用 GAS 函数的 Slack 机器人。一切正常,只是 Slack 显示错误消息,因为它在调用 API 时只等待 3 秒响应。

任何人都可以帮助我弄清楚如何异步运行 everyDay2 ,以便我可以在完成之前返回响应。我尝试过 Promise 和回调,但无法解决。

function doPost(e){

  const promise = new Promise(everyDay2);

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

}
Run Code Online (Sandbox Code Playgroud)

The*_*ter 5

承诺是行不通的。使用触发器代替

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

const runAfter1s = func =>
  ScriptApp.newTrigger(func)
    .timeBased()
    .after(1000)
    .create();
Run Code Online (Sandbox Code Playgroud)

everyDay2触发后一定要删除里面创建的触发器。