Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
var util = require("./util.js");
3
var isPrimitive = util.isPrimitive;
4
var wrapsPrimitiveReceiver = util.wrapsPrimitiveReceiver;
5
6
module.exports = function(Promise) {
7
var returner = function () {
8
return this;
9
};
10
var thrower = function () {
11
throw this;
12
};
13
var returnUndefined = function() {};
14
var throwUndefined = function() {
15
throw undefined;
16
};
17
18
var wrapper = function (value, action) {
19
if (action === 1) {
20
return function () {
21
throw value;
22
};
23
} else if (action === 2) {
24
return function () {
25
return value;
26
};
27
}
28
};
29
30
31
Promise.prototype["return"] =
32
Promise.prototype.thenReturn = function (value) {
33
if (value === undefined) return this.then(returnUndefined);
34
35
if (wrapsPrimitiveReceiver && isPrimitive(value)) {
36
return this._then(
37
wrapper(value, 2),
38
undefined,
39
undefined,
40
undefined,
41
undefined
42
);
43
}
44
return this._then(returner, undefined, undefined, value, undefined);
45
};
46
47
Promise.prototype["throw"] =
48
Promise.prototype.thenThrow = function (reason) {
49
if (reason === undefined) return this.then(throwUndefined);
50
51
if (wrapsPrimitiveReceiver && isPrimitive(reason)) {
52
return this._then(
53
wrapper(reason, 1),
54
undefined,
55
undefined,
56
undefined,
57
undefined
58
);
59
}
60
return this._then(thrower, undefined, undefined, reason, undefined);
61
};
62
};
63
64