Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
var util = require("./util.js");
3
var maybeWrapAsError = util.maybeWrapAsError;
4
var errors = require("./errors.js");
5
var TimeoutError = errors.TimeoutError;
6
var OperationalError = errors.OperationalError;
7
var haveGetters = util.haveGetters;
8
var es5 = require("./es5.js");
9
10
function isUntypedError(obj) {
11
return obj instanceof Error &&
12
es5.getPrototypeOf(obj) === Error.prototype;
13
}
14
15
var rErrorKey = /^(?:name|message|stack|cause)$/;
16
function wrapAsOperationalError(obj) {
17
var ret;
18
if (isUntypedError(obj)) {
19
ret = new OperationalError(obj);
20
ret.name = obj.name;
21
ret.message = obj.message;
22
ret.stack = obj.stack;
23
var keys = es5.keys(obj);
24
for (var i = 0; i < keys.length; ++i) {
25
var key = keys[i];
26
if (!rErrorKey.test(key)) {
27
ret[key] = obj[key];
28
}
29
}
30
return ret;
31
}
32
util.markAsOriginatingFromRejection(obj);
33
return obj;
34
}
35
36
function nodebackForPromise(promise) {
37
return function(err, value) {
38
if (promise === null) return;
39
40
if (err) {
41
var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
42
promise._attachExtraTrace(wrapped);
43
promise._reject(wrapped);
44
} else if (arguments.length > 2) {
45
var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
46
promise._fulfill(args);
47
} else {
48
promise._fulfill(value);
49
}
50
51
promise = null;
52
};
53
}
54
55
56
var PromiseResolver;
57
if (!haveGetters) {
58
PromiseResolver = function (promise) {
59
this.promise = promise;
60
this.asCallback = nodebackForPromise(promise);
61
this.callback = this.asCallback;
62
};
63
}
64
else {
65
PromiseResolver = function (promise) {
66
this.promise = promise;
67
};
68
}
69
if (haveGetters) {
70
var prop = {
71
get: function() {
72
return nodebackForPromise(this.promise);
73
}
74
};
75
es5.defineProperty(PromiseResolver.prototype, "asCallback", prop);
76
es5.defineProperty(PromiseResolver.prototype, "callback", prop);
77
}
78
79
PromiseResolver._nodebackForPromise = nodebackForPromise;
80
81
PromiseResolver.prototype.toString = function () {
82
return "[object PromiseResolver]";
83
};
84
85
PromiseResolver.prototype.resolve =
86
PromiseResolver.prototype.fulfill = function (value) {
87
if (!(this instanceof PromiseResolver)) {
88
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
89
}
90
this.promise._resolveCallback(value);
91
};
92
93
PromiseResolver.prototype.reject = function (reason) {
94
if (!(this instanceof PromiseResolver)) {
95
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
96
}
97
this.promise._rejectCallback(reason);
98
};
99
100
PromiseResolver.prototype.progress = function (value) {
101
if (!(this instanceof PromiseResolver)) {
102
throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
103
}
104
this.promise._progress(value);
105
};
106
107
PromiseResolver.prototype.cancel = function (err) {
108
this.promise.cancel(err);
109
};
110
111
PromiseResolver.prototype.timeout = function () {
112
this.reject(new TimeoutError("timeout"));
113
};
114
115
PromiseResolver.prototype.isResolved = function () {
116
return this.promise.isResolved();
117
};
118
119
PromiseResolver.prototype.toJSON = function () {
120
return this.promise.toJSON();
121
};
122
123
module.exports = PromiseResolver;
124
125