promise_resolver.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var util = require("./util.js");
  3. var maybeWrapAsError = util.maybeWrapAsError;
  4. var errors = require("./errors.js");
  5. var TimeoutError = errors.TimeoutError;
  6. var OperationalError = errors.OperationalError;
  7. var haveGetters = util.haveGetters;
  8. var es5 = require("./es5.js");
  9. function isUntypedError(obj) {
  10. return obj instanceof Error &&
  11. es5.getPrototypeOf(obj) === Error.prototype;
  12. }
  13. var rErrorKey = /^(?:name|message|stack|cause)$/;
  14. function wrapAsOperationalError(obj) {
  15. var ret;
  16. if (isUntypedError(obj)) {
  17. ret = new OperationalError(obj);
  18. ret.name = obj.name;
  19. ret.message = obj.message;
  20. ret.stack = obj.stack;
  21. var keys = es5.keys(obj);
  22. for (var i = 0; i < keys.length; ++i) {
  23. var key = keys[i];
  24. if (!rErrorKey.test(key)) {
  25. ret[key] = obj[key];
  26. }
  27. }
  28. return ret;
  29. }
  30. util.markAsOriginatingFromRejection(obj);
  31. return obj;
  32. }
  33. function nodebackForPromise(promise) {
  34. return function(err, value) {
  35. if (promise === null) return;
  36. if (err) {
  37. var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
  38. promise._attachExtraTrace(wrapped);
  39. promise._reject(wrapped);
  40. } else if (arguments.length > 2) {
  41. var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
  42. promise._fulfill(args);
  43. } else {
  44. promise._fulfill(value);
  45. }
  46. promise = null;
  47. };
  48. }
  49. var PromiseResolver;
  50. if (!haveGetters) {
  51. PromiseResolver = function (promise) {
  52. this.promise = promise;
  53. this.asCallback = nodebackForPromise(promise);
  54. this.callback = this.asCallback;
  55. };
  56. }
  57. else {
  58. PromiseResolver = function (promise) {
  59. this.promise = promise;
  60. };
  61. }
  62. if (haveGetters) {
  63. var prop = {
  64. get: function() {
  65. return nodebackForPromise(this.promise);
  66. }
  67. };
  68. es5.defineProperty(PromiseResolver.prototype, "asCallback", prop);
  69. es5.defineProperty(PromiseResolver.prototype, "callback", prop);
  70. }
  71. PromiseResolver._nodebackForPromise = nodebackForPromise;
  72. PromiseResolver.prototype.toString = function () {
  73. return "[object PromiseResolver]";
  74. };
  75. PromiseResolver.prototype.resolve =
  76. PromiseResolver.prototype.fulfill = function (value) {
  77. if (!(this instanceof PromiseResolver)) {
  78. throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
  79. }
  80. this.promise._resolveCallback(value);
  81. };
  82. PromiseResolver.prototype.reject = function (reason) {
  83. if (!(this instanceof PromiseResolver)) {
  84. throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
  85. }
  86. this.promise._rejectCallback(reason);
  87. };
  88. PromiseResolver.prototype.progress = function (value) {
  89. if (!(this instanceof PromiseResolver)) {
  90. throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
  91. }
  92. this.promise._progress(value);
  93. };
  94. PromiseResolver.prototype.cancel = function (err) {
  95. this.promise.cancel(err);
  96. };
  97. PromiseResolver.prototype.timeout = function () {
  98. this.reject(new TimeoutError("timeout"));
  99. };
  100. PromiseResolver.prototype.isResolved = function () {
  101. return this.promise.isResolved();
  102. };
  103. PromiseResolver.prototype.toJSON = function () {
  104. return this.promise.toJSON();
  105. };
  106. module.exports = PromiseResolver;