| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var Promise = require('./core.js');
|
|---|
| 4 |
|
|---|
| 5 | module.exports = Promise;
|
|---|
| 6 | Promise.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._state === 3) {
|
|---|
| 21 | return this._value.getValue();
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | if (!this.isFulfilled()) {
|
|---|
| 25 | throw new Error('Cannot get a value of an unfulfilled promise.');
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | return this._value;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | Promise.prototype.getReason = function () {
|
|---|
| 32 | if (this._state === 3) {
|
|---|
| 33 | return this._value.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._value;
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | Promise.prototype.getState = function () {
|
|---|
| 44 | if (this._state === 3) {
|
|---|
| 45 | return this._value.getState();
|
|---|
| 46 | }
|
|---|
| 47 | if (this._state === -1 || this._state === -2) {
|
|---|
| 48 | return 0;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return this._state;
|
|---|
| 52 | };
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | Promise.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 | };
|
|---|