Is google apps script synchronous?

后端 未结 3 706
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 10:08

I\'m a Java developer learning JavaScript and Google Apps Script simultaneously. Being the newbie I learned the syntax of JavaScript, not how it actually worked and I happil

3条回答
  •  暖寄归人
    2020-12-06 10:56

    I'm guessing from Google's point of view, that parallel processing two tasks (for example, that simply had Utilities.sleep(3000)) would require multiple threads to run in the server cpu, which may not be manageable and may be easy to abuse.

    Whereas parallel processing on the client or other companies server (e.g., Node.js) is up to that developer or user. (If they don't scale well it's not Google's problem)

    However there are some things that use parallelism


    UrlFetchApp.fetchAll

    UrlFetchApp.fetchAll() will asynchronously fetch many urls. Although this is not what you're truly looking for, fetching urls is a major reason to seek parallel processing.

    I'm guessing Google is reasoning this is ok since fetchall is using a web client and its own resources are already protected by quota.


    FirebaseApp getAllData

    Firebase I have found is very fast compared to using a spreadsheet for data storage. You can get many things from the database at once using FirebaseApp's getAllData:

    function myFunction() {
      var baseUrl = "https://samplechat.firebaseio-demo.com/";
      var secret = "rl42VVo4jRX8dND7G2xoI";
      var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
    
      // paths of 3 different user profiles
      var path1 = "users/jack";
      var path2 = "users/bob";
      var path3 = "users/jeane";
    
      Logger.log(database.getAllData([path1, path2, path3]));
    }
    

    HtmlService - IFrame mode

    HtmlService - IFrame mode allows full multi-tasking by going out to client script where promises are truly supported and making parallel calls back into the server. You can initiate this process from the server, but since all the parallel tasks' results are returned in the client, it's unclear how to get them back to the server. You could make another server call and send the results, but I'm thinking the goal would be to get them back to the script that called HtmlService in the first place, unless you go with a beginRequest and endRequest type architecture.


    tanaikech/RunAll

    This is a library for running the concurrent processing using only native Google Apps Script (GAS). This library claims full support via a RunAll.Do(workers) method.


    I'll update my answer if I find any other tricks.

提交回复
热议问题