synchronous_inspection.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. module.exports = function(Promise) {
  3. function PromiseInspection(promise) {
  4. if (promise !== undefined) {
  5. promise = promise._target();
  6. this._bitField = promise._bitField;
  7. this._settledValueField = promise._isFateSealed()
  8. ? promise._settledValue() : undefined;
  9. }
  10. else {
  11. this._bitField = 0;
  12. this._settledValueField = undefined;
  13. }
  14. }
  15. PromiseInspection.prototype._settledValue = function() {
  16. return this._settledValueField;
  17. };
  18. var value = PromiseInspection.prototype.value = function () {
  19. if (!this.isFulfilled()) {
  20. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  21. }
  22. return this._settledValue();
  23. };
  24. var reason = PromiseInspection.prototype.error =
  25. PromiseInspection.prototype.reason = function () {
  26. if (!this.isRejected()) {
  27. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  28. }
  29. return this._settledValue();
  30. };
  31. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  32. return (this._bitField & 33554432) !== 0;
  33. };
  34. var isRejected = PromiseInspection.prototype.isRejected = function () {
  35. return (this._bitField & 16777216) !== 0;
  36. };
  37. var isPending = PromiseInspection.prototype.isPending = function () {
  38. return (this._bitField & 50397184) === 0;
  39. };
  40. var isResolved = PromiseInspection.prototype.isResolved = function () {
  41. return (this._bitField & 50331648) !== 0;
  42. };
  43. PromiseInspection.prototype.isCancelled =
  44. Promise.prototype._isCancelled = function() {
  45. return (this._bitField & 65536) === 65536;
  46. };
  47. Promise.prototype.isCancelled = function() {
  48. return this._target()._isCancelled();
  49. };
  50. Promise.prototype.isPending = function() {
  51. return isPending.call(this._target());
  52. };
  53. Promise.prototype.isRejected = function() {
  54. return isRejected.call(this._target());
  55. };
  56. Promise.prototype.isFulfilled = function() {
  57. return isFulfilled.call(this._target());
  58. };
  59. Promise.prototype.isResolved = function() {
  60. return isResolved.call(this._target());
  61. };
  62. Promise.prototype.value = function() {
  63. return value.call(this._target());
  64. };
  65. Promise.prototype.reason = function() {
  66. var target = this._target();
  67. target._unsetRejectionIsUnhandled();
  68. return reason.call(target);
  69. };
  70. Promise.prototype._value = function() {
  71. return this._settledValue();
  72. };
  73. Promise.prototype._reason = function() {
  74. this._unsetRejectionIsUnhandled();
  75. return this._settledValue();
  76. };
  77. Promise.PromiseInspection = PromiseInspection;
  78. };