This question is answered at the following Stack Overflow link:
Wait for google server-side function to resolve
This question is specific to the google.script.run
API for Apps Script.
I need to use a google.script.run.serverFncName()
call to run code on the server, and then wait for the return before running the next function.
The code example that I am trying to convert, uses JavaScript fetch().then()
.
Example of code that I'm trying to convert:
<script>
window.some_Object_Name = {
innerNameOne : function() {
return fetch('The URL goes here for HTTP Request', {
method: 'post'
}).then(function(response_Back_From_Request) {
return res.json()response_Back_From_Request;
});
},
}
</script>
Need to use google.script.run.myServerAppsScriptFnc()
instead of fetch(url)
Obviously, something like the following won't work, but I'm showing it for illustration purposes:
<script>
window.some_Object_Name = {
innerNameOne : function() {
google.script.run
.withSuccessHandler(function(response_Back_From_Request) {return response_Back_From_Request;})
.myServerAppsScriptFnc({method:'post'});
.then(function(response_Back_From_Request) {
return response_Back_From_Request;
});
},
}
</script>
I'm assuming that I'll need to use a Promise
<script>
window.makeCallToGoogleServer = function() {
google.script.run
.withSuccessHandler(function(rtrnFromServer) {return rtrnFromServer;})
.myServerAppsScriptFnc({method:'post'});
}
window.some_Object_Name = {
innerNameOne : function() {
let myPromise = new Promise(function(myOnSuccessFnc) {
makeCallToGoogleServer();
myOnSuccessFnc(response_Back_From_Request);
});
myPromise.then(
return fromFirstFunc;
);
}
}
</script>