Reputation: 808
Consider side bar simple server side script .
const publicArr = [];
function doWait2(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function getfirstCall(){
publicArr.push("Item1");
doWait2(10000);
return publicArr;
}
function getsecondCall(){
publicArr.push("Item2");
return publicArr;
}
When calling getfirstCall
and getsecondCall
one after another, each function returns a single item array (Item1 / Item2 respectively). Changes made by getfirstCall
to publicArr
are not reflected when client execute getsecondCall
.
Note that getfirstCall
waits 10 sec before returning allowing getsecondCall
to be handled simultaneously by the server. not sure it is important.
Is this guarantee? each client call gets its own instances of the public variables?
Guess this is pretty fundamental with web development, and not only with side bar
Upvotes: 0
Views: 44
Reputation: 4419
In Apps Script global variables are not unique across script executions, you should consider using the PropertiesService to store your publicArr
as a script property, retrievable by all users. And perhaps LockService if you are worried about concurrent executions.
See these answers for pointers:
How to define global variable in Google Apps Script
https://stackoverflow.com/a/63601016/4243927
Upvotes: 1