3d3eeb17c8386e5201ee37f305cccffdcddabc3a
[aai/esr-gui.git] /
1 "use strict";
2 module.exports =
3 function(Promise, INTERNAL, tryConvertToPromise, apiRejection) {
4 var util = require("./util.js");
5 var tryCatch = util.tryCatch;
6
7 Promise.method = function (fn) {
8     if (typeof fn !== "function") {
9         throw new Promise.TypeError("fn must be a function\u000a\u000a    See http://goo.gl/916lJJ\u000a");
10     }
11     return function () {
12         var ret = new Promise(INTERNAL);
13         ret._captureStackTrace();
14         ret._pushContext();
15         var value = tryCatch(fn).apply(this, arguments);
16         ret._popContext();
17         ret._resolveFromSyncValue(value);
18         return ret;
19     };
20 };
21
22 Promise.attempt = Promise["try"] = function (fn, args, ctx) {
23     if (typeof fn !== "function") {
24         return apiRejection("fn must be a function\u000a\u000a    See http://goo.gl/916lJJ\u000a");
25     }
26     var ret = new Promise(INTERNAL);
27     ret._captureStackTrace();
28     ret._pushContext();
29     var value = util.isArray(args)
30         ? tryCatch(fn).apply(ctx, args)
31         : tryCatch(fn).call(ctx, args);
32     ret._popContext();
33     ret._resolveFromSyncValue(value);
34     return ret;
35 };
36
37 Promise.prototype._resolveFromSyncValue = function (value) {
38     if (value === util.errorObj) {
39         this._rejectCallback(value.e, false, true);
40     } else {
41         this._resolveCallback(value, true);
42     }
43 };
44 };