promise.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*!
  2. * Module dependencies
  3. */
  4. var MPromise = require('mpromise');
  5. var util = require('util');
  6. /**
  7. * Promise constructor.
  8. *
  9. * Promises are returned from executed queries. Example:
  10. *
  11. * var query = Candy.find({ bar: true });
  12. * var promise = query.exec();
  13. *
  14. * DEPRECATED. Mongoose 5.0 will use native promises by default (or bluebird,
  15. * if native promises are not present) but still
  16. * support plugging in your own ES6-compatible promises library. Mongoose 5.0
  17. * will **not** support mpromise.
  18. *
  19. * @param {Function} fn a function which will be called when the promise is resolved that accepts `fn(err, ...){}` as signature
  20. * @inherits mpromise https://github.com/aheckmann/mpromise
  21. * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
  22. * @event `err`: Emits when the promise is rejected
  23. * @event `complete`: Emits when the promise is fulfilled
  24. * @api public
  25. * @deprecated
  26. */
  27. function Promise(fn) {
  28. MPromise.call(this, fn);
  29. }
  30. /**
  31. * ES6-style promise constructor wrapper around mpromise.
  32. *
  33. * @param {Function} resolver
  34. * @return {Promise} new promise
  35. * @api public
  36. */
  37. Promise.ES6 = function(resolver) {
  38. var promise = new Promise();
  39. // No try/catch for backwards compatibility
  40. resolver(
  41. function() {
  42. promise.complete.apply(promise, arguments);
  43. },
  44. function(e) {
  45. promise.error(e);
  46. });
  47. return promise;
  48. };
  49. /*!
  50. * Inherit from mpromise
  51. */
  52. Promise.prototype = Object.create(MPromise.prototype, {
  53. constructor: {
  54. value: Promise,
  55. enumerable: false,
  56. writable: true,
  57. configurable: true
  58. }
  59. });
  60. /*!
  61. * ignore
  62. */
  63. Promise.prototype.then = util.deprecate(Promise.prototype.then,
  64. 'Mongoose: mpromise (mongoose\'s default promise library) is deprecated, ' +
  65. 'plug in your own promise library instead: ' +
  66. 'http://mongoosejs.com/docs/promises.html');
  67. /**
  68. * ES6-style `.catch()` shorthand
  69. *
  70. * @method catch
  71. * @memberOf Promise
  72. * @param {Function} onReject
  73. * @return {Promise}
  74. * @api public
  75. */
  76. Promise.prototype.catch = function(onReject) {
  77. return this.then(null, onReject);
  78. };
  79. /*!
  80. * Override event names for backward compatibility.
  81. */
  82. Promise.SUCCESS = 'complete';
  83. Promise.FAILURE = 'err';
  84. /**
  85. * Adds `listener` to the `event`.
  86. *
  87. * If `event` is either the success or failure event and the event has already been emitted, the`listener` is called immediately and passed the results of the original emitted event.
  88. *
  89. * @see mpromise#on https://github.com/aheckmann/mpromise#on
  90. * @method on
  91. * @memberOf Promise
  92. * @param {String} event
  93. * @param {Function} listener
  94. * @return {Promise} this
  95. * @api public
  96. */
  97. /**
  98. * Rejects this promise with `reason`.
  99. *
  100. * If the promise has already been fulfilled or rejected, not action is taken.
  101. *
  102. * @see mpromise#reject https://github.com/aheckmann/mpromise#reject
  103. * @method reject
  104. * @memberOf Promise
  105. * @param {Object|String|Error} reason
  106. * @return {Promise} this
  107. * @api public
  108. */
  109. /**
  110. * Rejects this promise with `err`.
  111. *
  112. * If the promise has already been fulfilled or rejected, not action is taken.
  113. *
  114. * Differs from [#reject](#promise_Promise-reject) by first casting `err` to an `Error` if it is not `instanceof Error`.
  115. *
  116. * @api public
  117. * @param {Error|String} err
  118. * @return {Promise} this
  119. */
  120. Promise.prototype.error = function(err) {
  121. if (!(err instanceof Error)) {
  122. if (err instanceof Object) {
  123. err = util.inspect(err);
  124. }
  125. err = new Error(err);
  126. }
  127. return this.reject(err);
  128. };
  129. /**
  130. * Resolves this promise to a rejected state if `err` is passed or a fulfilled state if no `err` is passed.
  131. *
  132. * If the promise has already been fulfilled or rejected, not action is taken.
  133. *
  134. * `err` will be cast to an Error if not already instanceof Error.
  135. *
  136. * _NOTE: overrides [mpromise#resolve](https://github.com/aheckmann/mpromise#resolve) to provide error casting._
  137. *
  138. * @param {Error} [err] error or null
  139. * @param {Object} [val] value to fulfill the promise with
  140. * @api public
  141. * @deprecated
  142. */
  143. Promise.prototype.resolve = function(err) {
  144. if (err) return this.error(err);
  145. return this.fulfill.apply(this, Array.prototype.slice.call(arguments, 1));
  146. };
  147. /**
  148. * Adds a single function as a listener to both err and complete.
  149. *
  150. * It will be executed with traditional node.js argument position when the promise is resolved.
  151. *
  152. * promise.addBack(function (err, args...) {
  153. * if (err) return handleError(err);
  154. * console.log('success');
  155. * })
  156. *
  157. * Alias of [mpromise#onResolve](https://github.com/aheckmann/mpromise#onresolve).
  158. *
  159. * _Deprecated. Use `onResolve` instead._
  160. *
  161. * @method addBack
  162. * @param {Function} listener
  163. * @return {Promise} this
  164. * @deprecated
  165. */
  166. Promise.prototype.addBack = Promise.prototype.onResolve;
  167. /**
  168. * Fulfills this promise with passed arguments.
  169. *
  170. * @method fulfill
  171. * @receiver Promise
  172. * @see https://github.com/aheckmann/mpromise#fulfill
  173. * @param {any} args
  174. * @api public
  175. * @deprecated
  176. */
  177. /**
  178. * Fulfills this promise with passed arguments.
  179. *
  180. * Alias of [mpromise#fulfill](https://github.com/aheckmann/mpromise#fulfill).
  181. *
  182. * _Deprecated. Use `fulfill` instead._
  183. *
  184. * @method complete
  185. * @receiver Promise
  186. * @param {any} args
  187. * @api public
  188. * @deprecated
  189. */
  190. Promise.prototype.complete = MPromise.prototype.fulfill;
  191. /**
  192. * Adds a listener to the `complete` (success) event.
  193. *
  194. * Alias of [mpromise#onFulfill](https://github.com/aheckmann/mpromise#onfulfill).
  195. *
  196. * _Deprecated. Use `onFulfill` instead._
  197. *
  198. * @method addCallback
  199. * @param {Function} listener
  200. * @return {Promise} this
  201. * @api public
  202. * @deprecated
  203. */
  204. Promise.prototype.addCallback = Promise.prototype.onFulfill;
  205. /**
  206. * Adds a listener to the `err` (rejected) event.
  207. *
  208. * Alias of [mpromise#onReject](https://github.com/aheckmann/mpromise#onreject).
  209. *
  210. * _Deprecated. Use `onReject` instead._
  211. *
  212. * @method addErrback
  213. * @param {Function} listener
  214. * @return {Promise} this
  215. * @api public
  216. * @deprecated
  217. */
  218. Promise.prototype.addErrback = Promise.prototype.onReject;
  219. /**
  220. * Creates a new promise and returns it. If `onFulfill` or `onReject` are passed, they are added as SUCCESS/ERROR callbacks to this promise after the nextTick.
  221. *
  222. * Conforms to [promises/A+](https://github.com/promises-aplus/promises-spec) specification.
  223. *
  224. * ####Example:
  225. *
  226. * var promise = Meetups.find({ tags: 'javascript' }).select('_id').exec();
  227. * promise.then(function (meetups) {
  228. * var ids = meetups.map(function (m) {
  229. * return m._id;
  230. * });
  231. * return People.find({ meetups: { $in: ids } }).exec();
  232. * }).then(function (people) {
  233. * if (people.length < 10000) {
  234. * throw new Error('Too few people!!!');
  235. * } else {
  236. * throw new Error('Still need more people!!!');
  237. * }
  238. * }).then(null, function (err) {
  239. * assert.ok(err instanceof Error);
  240. * });
  241. *
  242. * @see promises-A+ https://github.com/promises-aplus/promises-spec
  243. * @see mpromise#then https://github.com/aheckmann/mpromise#then
  244. * @method then
  245. * @memberOf Promise
  246. * @param {Function} onFulFill
  247. * @param {Function} onReject
  248. * @return {Promise} newPromise
  249. * @deprecated
  250. */
  251. /**
  252. * Signifies that this promise was the last in a chain of `then()s`: if a handler passed to the call to `then` which produced this promise throws, the exception will go uncaught.
  253. *
  254. * ####Example:
  255. *
  256. * var p = new Promise;
  257. * p.then(function(){ throw new Error('shucks') });
  258. * setTimeout(function () {
  259. * p.fulfill();
  260. * // error was caught and swallowed by the promise returned from
  261. * // p.then(). we either have to always register handlers on
  262. * // the returned promises or we can do the following...
  263. * }, 10);
  264. *
  265. * // this time we use .end() which prevents catching thrown errors
  266. * var p = new Promise;
  267. * var p2 = p.then(function(){ throw new Error('shucks') }).end(); // <--
  268. * setTimeout(function () {
  269. * p.fulfill(); // throws "shucks"
  270. * }, 10);
  271. *
  272. * @api public
  273. * @see mpromise#end https://github.com/aheckmann/mpromise#end
  274. * @method end
  275. * @memberOf Promise
  276. * @deprecated
  277. */
  278. /*!
  279. * expose
  280. */
  281. module.exports = Promise;