Google Apps脚本网络应用未按预期异步运行 [英] Google Apps Script web-app not running asynchronously as expected

查看:49
本文介绍了Google Apps脚本网络应用未按预期异步运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非阻塞(即无LockService)网络应用,该应用返回了存储在ScriptProperties中的计数器.

I've got a non-blocking (i.e. no LockService) web-app that returns a counter stored in ScriptProperties.

我在脚本中进行了人工睡眠,以测试对Web应用程序的异步调用.

I have an artificial sleep in the script to test asynchronous calls to the web-app.

看看下面的代码,我假设两次连续调用该Web应用程序应返回相同的数字.

Looking at this code below, I would assume that two successive calls to the web-app should return the same number.

但是,事实并非如此.两次调用该网络应用程序,一次又一次,返回的数字越来越多.这使我认为第一个电话在第二个电话运行之前就结束了-没道理.

However, it does not. Two calls to the web-app, one right after the other, returns increasing numbers. This makes me think the first call finishes before the second one runs -- which doesn't make sense.

function doGet(e)
{
    // get script properties
    var scriptProperties = PropertiesService.getScriptProperties();
    
    // get ID
    var id = parseInt(scriptProperties.getProperty("id"));

    // fake a long process
    // enough time to make another call to the web-app
    // in theory, the second call will get the same value for `id`
    Utilities.sleep(5000);

    // write a new value
    scriptProperties.setProperty("id", id + 1);
    
    // return it
    return ContentService.createTextOutput(id);
}

我正在尝试弄清楚为什么/为什么.Google不支持对网络应用程序的异步调用吗?

I am trying to figure out how/why. Does Google not support asynchronous calls for web-apps?

您可以在 https://script.google.com/macros上看到运行的/s/AKfycbxP6TQeMv_4b1lsYvGLA3YAn_reBhZ64Y2d04DotQ4CFJQtKhM/exec .

**更新**

这是我要测试的本地HTML文件.

Here is a local HTML file I am using to test.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script type="text/javascript">
        function doIt()
        {
            console.log("doIt: start");
            setTimeout(callIt, 500);
            console.log("doIt: end");
        }

        function callIt()
        {
            console.log("callIt: start");
            var request = new XMLHttpRequest();
            request.open('GET', 'https://script.google.com/macros/s/AKfycbxP6TQeMv_4b1lsYvGLA3YAn_reBhZ64Y2d04DotQ4CFJQtKhM/exec', true);

            request.onload = function()
            {
                if(this.status >= 200 && this.status < 400)
                {
                    document.querySelector("#output").innerText += this.response + ", ";
                }
                else
                {
                    alert("error");
                }
            };

            request.onerror = function()
            {
                alert("error");
            };

            request.send();
            console.log("callIt: end");
        }
    </script>
  </head>
  <body>
    output:
    <div id="output"></div>
    <input type="button" value="Click me" onclick="doIt()">
  </body>
</html>

非常快地单击按钮应该返回相同的数字,但是不会...

Clicking the button very quickly should return the same number, but it does not...

推荐答案

是的!诸如 doPost()之类的Web应用程序调用会异步运行.

Yes! Web Applications calls like doPost() run asynchrounously.

您无法复制冲突的原因是,相对于浏览器进行http调用所花费的时间,您的 sleep 时间太短了

The reason you were not able to replicate collision is because your sleep time was too low relative to the time taken by the browser to make http calls

  • sleep 延长至1分钟或更长时间.

  • Increase sleep to 1 minute or more.

Utilities.sleep(1*60*1000);

  • 如果您在下面的代码段脚本中使用了随机的睡眠时间,那么您也可以轻松证明异步性,该脚本除了显示随机的"callIt end"之外,还将显示每个循环的时间.

  • You can also easily prove asynchronicity, if you use a random sleep time with the below snippet script, which will show each loop's time in addition to random "callIt end"s.

    Utilities.sleep(Math.floor(Math.random()*60000)+1);
    

  • /*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:true});/*</ignore>*/
    function callIt(i) {
      const label = 'callIt' + i;
      console.log(label + ' start');
      console.time(label);
      const request = new XMLHttpRequest();
      request.open(
        'GET',
        'https://script.google.com/macros/s/AKfycbxP6TQeMv_4b1lsYvGLA3YAn_reBhZ64Y2d04DotQ4CFJQtKhM/exec',
        true
      );
    
      request.onload = function() {
        if (this.status >= 200 && this.status < 400) {
          console.log(this.response + `- ${label}`);
          console.log(label + ' end');
          console.timeEnd(label);
          console.log('\n')
        } else {
          alert('error');
        }
      };
    
      request.onerror = function() {
        alert('error');
      };
    
      request.send();
    }
    let i = 0;
    while (++i < 10) callIt(i);

    <!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

    这篇关于Google Apps脚本网络应用未按预期异步运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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