Using promises rather than callbacks is a much cleaner way of dealing with asynchronous activities. Promises were introduced in EcmaScript 6, and of course are not implemented in Server side Apps Script. Since everything is synchronous there anyway, it’s not too big of a deal.

However you can use Promises for client side code when using HtmlService – the IFrame mode allows it to run just fine. You just need to make sure you include a polyfill, in case it’s running on an old browser.

Using promises with

If you use, you’ll know this is a way to make calls to server side functions. What’s really good about this is that it’s asynch, so this means you can make multiple calls simultaneously – a great way to make your web apps more responsive, but it’s a bit messy since you have to have global functions to call, and you also have to deal with callbacks.

But you can use promises instead – here’s how.

An example – this runs Server.getData (100);

What’s nice about this approach is that returns a promise, so you can use things like promise.all() to wait until a number of tasks have all completed – for example (a null namespace argument means its a global server side function)

Later on I’ll show some example of some cool webapps using this approach.

The code

In your html file you’ll need the polyfill in case it’s running on old browsers

In your client side code you’ll need this provoke namespace

and you’ll also need this server side global function.


If you want to make sure that only certain methods or namespaces are ever invoked from the client side with exposeRun, then you can do that like this example. For example my Apps only ever allow functions in the Server namespace to be provoked, so I have a slightly modified version of exposeRun

And that’s it, you’re using Promises to call Apps Script Server side functions!

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.