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
|
"use strict";
var util = require("./util.js");
var isPrimitive = util.isPrimitive;
module.exports = function(Promise) {
var returner = function () {
return this;
};
var thrower = function () {
throw this;
};
var returnUndefined = function() {};
var throwUndefined = function() {
throw undefined;
};
var wrapper = function (value, action) {
if (action === 1) {
return function () {
throw value;
};
} else if (action === 2) {
return function () {
return value;
};
}
};
Promise.prototype["return"] =
Promise.prototype.thenReturn = function (value) {
if (value === undefined) return this.then(returnUndefined);
if (isPrimitive(value)) {
return this._then(
wrapper(value, 2),
undefined,
undefined,
undefined,
undefined
);
}
return this._then(returner, undefined, undefined, value, undefined);
};
Promise.prototype["throw"] =
Promise.prototype.thenThrow = function (reason) {
if (reason === undefined) return this.then(throwUndefined);
if (isPrimitive(reason)) {
return this._then(
wrapper(reason, 1),
undefined,
undefined,
undefined,
undefined
);
}
return this._then(thrower, undefined, undefined, reason, undefined);
};
};
|