Google AppMaker中的异步功能 [英] Asynchronous functions in Google AppMaker

查看:87
本文介绍了Google AppMaker中的异步功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数BigQueryGetEmail(),可从BigQuery查询请求者管理器信息.我调用代码以获取信息并将其传递给变量.但是问题在于,因为JS是单线程语言,所以我认为它在完成查询所需信息之前会跳过我调用BigQueryGetEmail()函数的代码.效果是,当我将其分配给变量以确保其将返回错误时,因为还没有数据.

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.

我试图将变量赋值放入BigQueryGetEmail()的回调中,但仍然没有时间捕获数据.还尝试了异步功能,但似乎不适用于AppMaker.测试时,查询功能按预期方式工作.它返回请求者经理的电子邮件.

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) { });

我希望查询将首先获取请求者管理者的电子邮件地址,然后在保存记录之前将值首先传递给draft.Email.

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.

推荐答案

客户端api google.script.run 具有故障处理程序和成功处理程序.要详细了解其工作原理,请参阅官方文档.

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);

这篇关于Google AppMaker中的异步功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆