I've got a non-blocking (i.e. no LockService) web-app that returns a counter stored in ScriptProperties.
I have an artificial sleep in the script to test asynchronous calls to the web-app.
Looking at this code below, I would assume that two successive calls to the web-app should return the same number.
However, it does not. Two calls to the web-app, one right after the other, returns increasing numbers. This makes me think the first call finishes before the second one runs -- which doesn't make sense.
function doGet(e)
{
// get script properties
var scriptProperties = PropertiesService.getScriptProperties();
// get ID
var id = parseInt(scriptProperties.getProperty("id"));
// fake a long process
// enough time to make another call to the web-app
// in theory, the second call will get the same value for `id`
Utilities.sleep(5000);
// write a new value
scriptProperties.setProperty("id", id + 1);
// return it
return ContentService.createTextOutput(id);
}
I am trying to figure out how/why. Does Google not support asynchronous calls for web-apps?
You can see this running at https://script.google.com/macros/s/AKfycbxP6TQeMv_4b1lsYvGLA3YAn_reBhZ64Y2d04DotQ4CFJQtKhM/exec.
** UPDATE **
Here is a local HTML file I am using to test.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script type="text/javascript">
function doIt()
{
console.log("doIt: start");
setTimeout(callIt, 500);
console.log("doIt: end");
}
function callIt()
{
console.log("callIt: start");
var request = new XMLHttpRequest();
request.open('GET', 'https://script.google.com/macros/s/AKfycbxP6TQeMv_4b1lsYvGLA3YAn_reBhZ64Y2d04DotQ4CFJQtKhM/exec', true);
request.onload = function()
{
if(this.status >= 200 && this.status < 400)
{
document.querySelector("#output").innerText += this.response + ", ";
}
else
{
alert("error");
}
};
request.onerror = function()
{
alert("error");
};
request.send();
console.log("callIt: end");
}
</script>
</head>
<body>
output:
<div id="output"></div>
<input type="button" value="Click me" onclick="doIt()">
</body>
</html>
Clicking the button very quickly should return the same number, but it does not...
UrlFetchApp.fetchAll([{url: url}, {url: url}])is used, 2 same values are returned.Does Google not support asynchronous calls for web-apps?in your question and your additional script, I thought that your question might have 2 questions. So at first, aboutDoes Google not support asynchronous calls for web-apps?in your question, how about confirming this? In order to check it, as the simple script, you can do it using the Google Apps Script ofUrlFetchApp.fetchAll([{url: url}, {url: url}]).forEach(e => console.log(e.getContentText()));. As the next step, your script is considered. How about this?