Last active
July 17, 2019 08:10
-
-
Save shrugs/44cfb94faa7f09bcd9cb to your computer and use it in GitHub Desktop.
google.script.run promise wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// turns google.script.run into an object with a promise api | |
function scriptRunPromise() | |
var gs = {}; | |
var ks = Object.keys(google.script.run); | |
for (var i=0; i < ks.length; i++) { | |
gs[ks[i]] = (function(k) { | |
return function() { | |
var args = arguments; | |
return Promise(function(resolve, reject) { | |
google.script.run | |
.withSuccessHandler(resolve) | |
.withFailureHandler(reject) | |
[k].apply(google.script.run, args); | |
}); | |
} | |
})(ks[i]) | |
} | |
return gs; | |
} | |
// USAGE: | |
var g = scriptRunPromise(); | |
g.myServerMethod().then(function(result) { | |
// blah | |
}) | |
.catch(function(e) { | |
// blah | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just in case anybody still tries to get this to work, line 9 is missing the new operator when generating the promise.
As an Angular service:
Usage: