本文介绍了使用Javascript异步&等待google.script.url.getLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Apps脚本Web应用程序中重构一些难看的代码,以便它使用 async / await .

I am trying to refactor some ugly code in a Google Apps Script web application so that it uses async / await.

它使用 google.script.url.getLocation 客户端提取URL参数,然后将其发送给其他异步函数.

It uses the google.script.url.getLocation client-side, to pull URL parameters and then send them off to other async functions.

一定有一种优雅的方法.

There must be a way to do this elegantly.

var  doSomeAsyncShit =()=> {
    google.script.url.getLocation(function (location) {

      var rid = (location.parameter.rid) ? location.parameter.rid : defaultReportID;
      var uid = (location.parameter.uid) ? location.parameter.uid : defaultUserID;

      console.log (((location.parameter.rid) ? "Report #" : "Default Report ID #")+rid);
      console.log (((location.parameter.uid) ? "User #" : "Default User ID #" )+uid);

      google.script.run.withSuccessHandler(paintReport).returnJSON(rid);
      google.script.run.withSuccessHandler(getMyReportsList).listMyReports(uid);
    });

  }

  

  $(function () {

    doSomeAsyncShit();
}

推荐答案

由于 Promise 可以使用自定义执行程序函数构建,您可以将 google.script.url 包装到其中并解析或随时随地拒绝.如果随后将其设为实用程序功能,请使用 await 等待其解决.

Since a Promise can be constructed with a custom executor function, you can wrap the google.script.url into it and resolve or reject whenever you like. If you then make it a utility function, use await to wait for it to resolve.

以下是一个灵活的小型实用程序,用于使 google.script.url 异步友好:

Below is a small flexible utility for making google.script.url async-friendly:

/**
 * @typedef {{
 *  hash : string,
 *  parameter : Object.<string, string>,
 *  parameters : Object.<string, string[]>
 * }} UrlLocationObject
 * 
 * @typedef {{
 *  callback : function (UrlLocationObject, ...any) : any,
 *  params : any[]
 * }} AsyncUrlOptions
 * 
 * @summary Promise-friendly google.script.url
 * @param {AsyncUrlOptions}
 * @returns {Promise}
 */
const asyncLocation = ({
    callback,
    params = [],
}) => {
    return new Promise((res, rej) => {
        google.script.url.getLocation((loc) => {
            try {
                const result = callback(loc, ...params);
                res(result);
            }
            catch(error) {
                rej(error);
            }
        });
    });
};

google.script.run 也是如此:

/**
 * @typedef {{
 *  funcName : string,
 *  onFailure : function,
 *  onSuccess : function,
 *  params : array
 * }} AsyncOptions
 * 
 * @summary v2 of async-friendly google.script.run
 * @param {AsyncOptions}
 * @returns {Promise}
 */
const asyncGAPIv2 = ({
    funcName,
    onFailure = console.error,
    onSuccess,
    params = []
}) => {
    return new Promise((res, rej) => {
        google.script.run
            .withSuccessHandler(data => {
                typeof onSuccess === "function" && onSuccess(data);
                res(data);
            })
            .withFailureHandler(error => {
                typeof onFailure === "function" && onFailure(error);
                rej(error);
            })
        [funcName].apply(null, params);
    });
};

这篇关于使用Javascript异步&amp;等待google.script.url.getLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:52