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.
If you use
But you can use promises instead - here's how.
An example - this runs
What's nice about this approach is that Provoke.run 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.
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.
You want to learn Google Apps Script?
Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >