forked from andrewroberts/RoseTaskManager-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvoker.js.html
79 lines (63 loc) · 1.7 KB
/
Provoker.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<script>
/**
* used to expose memebers of a namespace
* @param {string} namespace name
* @param {method} method name
*/
function exposeRun(namespace, method, argArray) {
var func = namespace ? this[namespace][method] : this[method];
if (argArray && argArray.length) {
return func.apply(this, argArray);
} else {
return func();
}
}
/**
* @namespace Provoke
* promise management for async calls
*/
var Provoke = (function (ns) {
/**
* run something asynchronously
* @param {string} namespace the namespace (null for global)
* @param {string} method the method or function to call
* @param {[...]} the args
* @return {Promise} a promise
*/
ns.run = function (namespace, method) {
// the args to the server function
var runArgs = Array.prototype.slice.call(arguments).slice(2);
if (arguments.length < 2) {
throw new Error ('need at least a namespace and method');
}
// this will return a promise
return new Promise(function ( resolve , reject ) {
google.script.run
.withFailureHandler (function(err) {
reject (err);
})
.withSuccessHandler (function(result) {
resolve (result);
})
.exposeRun (namespace, method, runArgs);
});
};
/*
* settimeout as a promise
* @param {number} ms number of ms to wait
* @param {*} [tag] optional tag that will be returned when resolved
*/
ns.loiter = function (ms, tag) {
return new Promise(function(resolve, reject) {
try {
setTimeout(function() {
resolve(tag);
}, ms);
} catch (err) {
reject(err);
}
});
};
return ns;
})(Provoke || {});
</script>