synchronous.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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._65 === 3) {
  16. return this._55.getValue();
  17. }
  18. if (!this.isFulfilled()) {
  19. throw new Error('Cannot get a value of an unfulfilled promise.');
  20. }
  21. return this._55;
  22. };
  23. Promise.prototype.getReason = function () {
  24. if (this._65 === 3) {
  25. return this._55.getReason();
  26. }
  27. if (!this.isRejected()) {
  28. throw new Error('Cannot get a rejection reason of a non-rejected promise.');
  29. }
  30. return this._55;
  31. };
  32. Promise.prototype.getState = function () {
  33. if (this._65 === 3) {
  34. return this._55.getState();
  35. }
  36. if (this._65 === -1 || this._65 === -2) {
  37. return 0;
  38. }
  39. return this._65;
  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. };