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