direct_resolve.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. var util = require("./util.js");
  3. var isPrimitive = util.isPrimitive;
  4. var wrapsPrimitiveReceiver = util.wrapsPrimitiveReceiver;
  5. module.exports = function(Promise) {
  6. var returner = function () {
  7. return this;
  8. };
  9. var thrower = function () {
  10. throw this;
  11. };
  12. var returnUndefined = function() {};
  13. var throwUndefined = function() {
  14. throw undefined;
  15. };
  16. var wrapper = function (value, action) {
  17. if (action === 1) {
  18. return function () {
  19. throw value;
  20. };
  21. } else if (action === 2) {
  22. return function () {
  23. return value;
  24. };
  25. }
  26. };
  27. Promise.prototype["return"] =
  28. Promise.prototype.thenReturn = function (value) {
  29. if (value === undefined) return this.then(returnUndefined);
  30. if (wrapsPrimitiveReceiver && isPrimitive(value)) {
  31. return this._then(
  32. wrapper(value, 2),
  33. undefined,
  34. undefined,
  35. undefined,
  36. undefined
  37. );
  38. }
  39. return this._then(returner, undefined, undefined, value, undefined);
  40. };
  41. Promise.prototype["throw"] =
  42. Promise.prototype.thenThrow = function (reason) {
  43. if (reason === undefined) return this.then(throwUndefined);
  44. if (wrapsPrimitiveReceiver && isPrimitive(reason)) {
  45. return this._then(
  46. wrapper(reason, 1),
  47. undefined,
  48. undefined,
  49. undefined,
  50. undefined
  51. );
  52. }
  53. return this._then(thrower, undefined, undefined, reason, undefined);
  54. };
  55. };