问题
I have a function BigQueryGetEmail()
that query requester Manager info from BigQuery. I call the code in order to get the info and pass it to a variable. But the problem is because JS is a single threaded language, so I think that it skips the code where I call the BigQueryGetEmail()
function before it finish querying the info that I need. The effect is when I assign it to a variable for sure it will return an error because there is no data yet.
I have tried to put the variable assignment inside the callback of the BigQueryGetEmail()
yet still not have time to capture data. Also tried async function but it doesn't seem like it works on AppMaker. The query function is working as expected when I test it out. It returns the requester Manager's email.
//run Query to get Manager email info base on requester email
var email = google.script.run.BigQueryGetEmail(emailRequester);
//assign email value to draft.email;
draft.Email = email;
//create record
createDatasource.createItem(function(createdRecord) { });
I expect the query will get the requester Manager's email address first, then pass the value to draft.Email
first before save the record.
回答1:
The client-side api google.script.run has a failure handler and a success handler. To read more about how it work please refer to the official documentation.
Basically you need to reorganize your code to look something like this:
function successHandler(email){
//assign email value to draft.email;
draft.Email = email;
//create record
createDatasource.createItem(function(createdRecord) { });
}
function failureHandler(error){
console.log(error);
}
//run Query to get Manager email info base on requester email
google.script.run.withSuccessHandler(successHandler).withFailureHandler(failureHandler).BigQueryGetEmail(emailRequester);
来源:https://stackoverflow.com/questions/54178050/asynchronous-functions-in-google-appmaker