UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2
3var Promise = require('./core.js');
4
5module.exports = Promise;
6Promise.enableSynchronous = function () {
7 Promise.prototype.isPending = function() {
8 return this.getState() == 0;
9 };
10
11 Promise.prototype.isFulfilled = function() {
12 return this.getState() == 1;
13 };
14
15 Promise.prototype.isRejected = function() {
16 return this.getState() == 2;
17 };
18
19 Promise.prototype.getValue = function () {
20 if (this._i === 3) {
21 return this._j.getValue();
22 }
23
24 if (!this.isFulfilled()) {
25 throw new Error('Cannot get a value of an unfulfilled promise.');
26 }
27
28 return this._j;
29 };
30
31 Promise.prototype.getReason = function () {
32 if (this._i === 3) {
33 return this._j.getReason();
34 }
35
36 if (!this.isRejected()) {
37 throw new Error('Cannot get a rejection reason of a non-rejected promise.');
38 }
39
40 return this._j;
41 };
42
43 Promise.prototype.getState = function () {
44 if (this._i === 3) {
45 return this._j.getState();
46 }
47 if (this._i === -1 || this._i === -2) {
48 return 0;
49 }
50
51 return this._i;
52 };
53};
54
55Promise.disableSynchronous = function() {
56 Promise.prototype.isPending = undefined;
57 Promise.prototype.isFulfilled = undefined;
58 Promise.prototype.isRejected = undefined;
59 Promise.prototype.getValue = undefined;
60 Promise.prototype.getReason = undefined;
61 Promise.prototype.getState = undefined;
62};