Article
home/blog/Loading

Use promises in Google Apps Scripts

To use the latest es6 Promises in google app scripts it is very trivial to do so.

You need to include the Promises in the polyfill

Including Promises

Include the following code at the top of your file which you wish to use promises in.

var Promise,
    setTimeout = setTimeout || function (func,ms) {
      Utilities.sleep(ms);
      func();
    };

(function () {  

  // get the polyfill and eval
  if (!Promise) {
    var result = UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.1/es6-promise.min.js');
    eval (result.getContentText());

    // add done for compatibility with other promise systems
    Promise.prototype.done = Promise.prototype.done || Promise.prototype.then ;

  }

}());