Is Google Apps script synchronous?
I am a Java developer learning JavaScript and Google Apps Script at the same time. As a beginner, I learned JavaScript syntax, not how it actually worked, and I happily hacked into Google Apps Script and wrote the code sequentially and synchronously like Java. My whole code resembles this: (crudely simplified to show what I mean)
function doStuff() {
var url = 'https://myCompany/api/query?term<term&search';
var json = getJsonFromAPI(url);
Logger.log(json);
}
function getJsonFromAPI(url) {
var response = UrlFetchApp.fetch(url);
var json = JSON.parse(response);
return json;
}
And it works! It works great! If I hadn't studied JavaScript, I would have said that it works like a clockwork. But JavaScript is not a clockwork, it is nicely asynchronous and from what I understand it shouldn't work at all, it "compiles", but the variable logging json
should log undefined, but it logs JSON without issue.
Note:
The code is written and executed in the <Literature> Script editor.
Why is this?
source to share
While Google Apps Script implements a subset of ECMAScript 5 , nothing makes it asynchronous.
While it is true that the main strength of JavaScript is its asynchronous nature, the Google developers seem to have given this in favor of a simpler and simpler API.
UrlFetchApp
are synchronous. They return an object HttpResponse
and they don't accept a callback. This appears to be an API solution.
source to share