google.script.run.withSuccessHandler and withFailureHandler returned before script.run is completed

2,799 views
Skip to first unread message

Alexander Kozlovsky

unread,
Aug 25, 2022, 5:24:30 AM8/25/22
to Google Apps Script Community
Dear Developer community,

I'm currently developing an Application for a client to interact with the GTM API trough a Front End. 
I have run into an issue with the callback handlers withSuccessHandler and withFailureHandler both are returned before the google.script.run function is completed.

Within my .HTML I have defined the following function: 

function runUpdate(tagId,tagIndex) {
  var id = 'tagInfo'+tagId
  console.log(id)
  
  google.script.run
  .withSuccessHandler(onSuccess(id))
  .withFailureHandler(onFailure(id)) 
  .updateTag(<?=acc?>,<?=con?>,<?=wsp?>, tagId,<?=gaawc?>)
  
  console.log("Script.run completed")
}
     
function onSuccess(tagIndex){
      console.log("Success Handler triggered")

    document.getElementById(tagIndex).innerHTML = "Success"
}
  
  function onFailure(tagIndex){
      console.log("Failure Handler triggered")

    document.getElementById(tagIndex).innerHTML = "ERROR"
}

runUpdate is triggered on a button click. 
The runUpdate() function is located in the Code.gs file and looks similar to this:

function updateTag(acc, con, wsp, tagId, gaawc) {

  var oldTag = TagManager.Accounts.Containers.Workspaces.Tags.get("accounts/" + acc + "/containers/" + con + "/workspaces/" + wsp + "/tags/" + tagId)
  if (oldTag){
    Logger.log(JSON.stringify(oldTag))
  
  var response = TagManager.Accounts.Containers.Workspaces.Tags.create(TranslateTag(oldTag, gaawc), "accounts/" + acc + "/containers/" + con + "/workspaces/" + wsp)
 }
}

As per the documentation I expected .withSuccessHandler() and withFailureHandler() be fired accordingly to the outcome of the server side function.

The outcome is slightly different as the Log shows, the Handlers are being fired immediately after the runUpdate function is started.

userCodeAppPanel:2  function started
userCodeAppPanel:5 Failure Handler triggered
userCodeAppPanel:5 Success Handler triggered
userCodeAppPanel:25 Script.run completed
445144638-warden_bin_i18n_warden__en_gb.js:99 Net state changed from IDLE to BUSY
4078442995-mae_html_user_bin_i18n_mae_html_user__de.js:128 dropping postMessage.. deserialize threw error.
445144638-warden_bin_i18n_warden__en_gb.js:99 Net state changed from BUSY to IDLE
4078442995-mae_html_user_bin_i18n_mae_html_user__de.js:58 Uncaught  at updateTag (translateTag:12)

The result is unfortunately that the last defined Handler would be overwriting the returned message Success/Failure.

Looking for any advice.

Bruce Mcpherson

unread,
Aug 25, 2022, 5:44:34 AM8/25/22
to google-apps-sc...@googlegroups.com
Both the handlers are being triggered because you're calling them rather than referencing them

  .withSuccessHandler(onSuccess(id))
  .withFailureHandler(onFailure(id)) 

should be 
  .withSuccessHandler(onSuccess)
  .withFailureHandler(onFailure) 

and they will receive whatever the response passed back from the script running server side

Also - what are you trying to achieve here?
  .updateTag(<?=acc?>,<?=con?>,<?=wsp?>, tagId,<?=gaawc?>)

If acc,con,wsp,and gaawc are server side global variables, then there's no need to try to pass them back
  .withSuccessHandler(onSuccess)
  .withFailureHandler(onFailure) 
  .updateTag( tagId)

will do it with
function updateTag(tagId) {...}

also
 console.log("Script.run completed") will run before updateTag because google.script.run is asynchronous. In other words it'll do the log while it's still working on executing updateTag.
You'd need to put console.log("Script.run completed")  in your successHandler function rather than in the main body.

also 
if you need to pass additional arguments to your success and error handlers, you could do this
  .withSuccessHandler((serverResponse) => onSuccess(id, serverResponse))
  .withFailureHandler((serverError) => onFailure(id, serverError)) 
  .updateTag( tagId)

const onSuccess  = (id,serverResponse) => {
   // do something with the id and the response
}
etc...



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4985067b-cbe2-44d0-8855-26419a547486n%40googlegroups.com.

Alexander Kozlovsky

unread,
Aug 26, 2022, 7:45:16 AM8/26/22
to Google Apps Script Community
This makes perfect sense, thank you for your assistance.
Reply all
Reply to author
Forward
0 new messages