最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does Google AppscriptUrlFetchApp.fetchAll() function run synchronously? - Stack Overflow

programmeradmin2浏览0评论

The problem I'm running into is that I have a number of API requests I want to make, all of which take several seconds to return because the size of the data being returned, and UrlFetchApp.fetchAll(..) is simply returning an array of empty JS objects, eg: [{}, {}, {}, ...].

My requests array look something like this (formatted for clarity):

requests = [
  {
    validateHttpsCertificates: false,
    url: ';params2=true'
  },
  {
    validateHttpsCertificates: false,
    url: ';params4=true'
  }
];

The code to make my requests:

responses = UrlFetchApp.fetchAll(requests);

// returns back '[{}, {}]'
console.log(JSON.stringify(responses));

I can confirm through the Database that the API calls are being run, since AWS RDS Performance Metrics show the DB queries run, and I can also confirm that the API itself is responding with 200's via NewRelic, which is why my hunch is that I'm not using GAS/UrlFetchApp.fetchAll() correctly.

So, I am wondering:

  1. Does GAS run synchronously, aka, it'll wait for .fetchAll() to return before running the console.log(...) line?
  2. Am I actually calling fetchAll correctly? Currently at a loss, and the Google Appscript documentation is meager at best.

Thank you in advance for the help.

EDIT:

I migrated to fetchAll after I successfully used fetch, eg:

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

The problem I'm running into is that I have a number of API requests I want to make, all of which take several seconds to return because the size of the data being returned, and UrlFetchApp.fetchAll(..) is simply returning an array of empty JS objects, eg: [{}, {}, {}, ...].

My requests array look something like this (formatted for clarity):

requests = [
  {
    validateHttpsCertificates: false,
    url: 'https://url./api/v2/api_key/endpoint/action?params1=false&params2=true'
  },
  {
    validateHttpsCertificates: false,
    url: 'https://url./api/v2/api_key/endpoint/action?params3=false&params4=true'
  }
];

The code to make my requests:

responses = UrlFetchApp.fetchAll(requests);

// returns back '[{}, {}]'
console.log(JSON.stringify(responses));

I can confirm through the Database that the API calls are being run, since AWS RDS Performance Metrics show the DB queries run, and I can also confirm that the API itself is responding with 200's via NewRelic, which is why my hunch is that I'm not using GAS/UrlFetchApp.fetchAll() correctly.

So, I am wondering:

  1. Does GAS run synchronously, aka, it'll wait for .fetchAll() to return before running the console.log(...) line?
  2. Am I actually calling fetchAll correctly? Currently at a loss, and the Google Appscript documentation is meager at best.

Thank you in advance for the help.

EDIT:

I migrated to fetchAll after I successfully used fetch, eg:

// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
Share Improve this question edited Nov 12, 2019 at 23:28 Stu asked Nov 12, 2019 at 23:14 StuStu 1093 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

How about this answer?

Answer for Question 1:

The fetchAll method works with the asynchronous processing. Ref If you want to use UrlFetchApp with the synchronous processing, please use UrlFetchApp.fetch() in a loop.

Answer for Question 2:

I think that your request for the fetchAll method is correct. In order to retrieve the responses from UrlFetchApp.fetchAll(requests), how about the following modification?

Modified script:

var responses = UrlFetchApp.fetchAll(requests);
var res = responses.map(function(e) {return e.getContentText()});
console.log(JSON.stringify(res)); //  or Logger.log(JSON.stringify(res));
  • In this modification, getContentText() is used for each response.
  • The order of responses is the same with the order of requests.

References:

  • getContentText()
  • fetchAll(requests)
  • Benchmark: fetchAll method in UrlFetch service for Google Apps Script

If I misunderstood your question and this was not the result you want, I apologize.

发布评论

评论列表(0)

  1. 暂无评论