0

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>
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Forgive my ignorance, but why doesn't the `withSuccessHandler()` call work? (without the use of `then()`) – Diego Dec 09 '20 at 14:11
  • You'd need to pass `myOnSuccessFnc` as the `withSuccessHandler` [like this](https://gist.github.com/shrugs/44cfb94faa7f09bcd9cb). But really, does google-apps-script still not provide a promisified api? – Bergi Dec 09 '20 at 14:48
  • https://ramblings.mcpher.com/gassnippets2/organizing-asynchronous-calls-to-google-script-run/#Organizing_with_promises – Bergi Dec 09 '20 at 14:51
  • Thanks to @Bergi and the link to Desktop Liberation I was able to patch together a solution. Should I add my solution that is specific to Apps Script to the 20 other answers in the post that is supposed to be a duplicate of my question? – Alan Wells Dec 09 '20 at 20:33
  • @AlanWells not on the generic "*how to convert an API to promises*", no. On the other one, why not. There's also [Easiest way to wait google server-side function resolve](https://stackoverflow.com/questions/58387340/easiest-way-to-wait-google-server-side-function-resolve) which would be an even better duplicate, but had only a horrible answer (e.g. no error handling) so I had not linked it. If you want to add a good answer there, I'll make sure to upvote it! – Bergi Dec 09 '20 at 21:23
  • The answer that I created for this question is given at the following Stack Overflow link: [https://stackoverflow.com/a/65226874/2946873](https://stackoverflow.com/a/65226874/2946873) My answer includes error handling and two `then()` methods. – Alan Wells Dec 10 '20 at 00:36

0 Answers0