小编Stu*_*Stu的帖子

Google Appscript / UrlFetchApp.fetchAll()函数是否可以同步运行?

我遇到的问题是,我要发出许多API请求,由于要返回的数据大小,所有这些请求都需要花费几秒钟的时间才能返回,并且UrlFetchApp.fetchAll(..)仅返回一个空JS对象数组,例如:[{}, {}, {}, ...]

我的请求数组看起来像这样(为清楚起见而格式化):

requests = [
  {
    validateHttpsCertificates: false,
    url: 'https://url.com/api/v2/api_key/endpoint/action?params1=false&params2=true'
  },
  {
    validateHttpsCertificates: false,
    url: 'https://url.com/api/v2/api_key/endpoint/action?params3=false&params4=true'
  }
];
Run Code Online (Sandbox Code Playgroud)

发出我的请求的代码:

responses = UrlFetchApp.fetchAll(requests);

// returns back '[{}, {}]'
console.log(JSON.stringify(responses));
Run Code Online (Sandbox Code Playgroud)

我可以通过数据库确认正在运行API调用,因为AWS RDS性能指标显示数据库查询正在运行,并且我还可以确认API本身通过NewRelic响应为200,这就是我的直觉是m未UrlFetchApp.fetchAll()正确使用GAS / 。

因此,我想知道:

  1. GAS是否同步运行,也就是.fetchAll()在运行console.log(...)线路之前会等待返回?
  2. 我真的打fetchAll正确吗?目前不知所措,而Google Appscript文档充其量是微不足道的。

预先感谢您的帮助。

编辑:

我成功使用fetchAll 迁移到fetch,例如:

// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
Run Code Online (Sandbox Code Playgroud)

javascript google-apps-script google-data-studio

1
推荐指数
1
解决办法
45
查看次数