You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

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