synchronous.js 1.4 KB

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