wrapThunk.js 907 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const MongooseError = require('../../error/mongooseError');
  3. /*!
  4. * A query thunk is the function responsible for sending the query to MongoDB,
  5. * like `Query#_findOne()` or `Query#_execUpdate()`. The `Query#exec()` function
  6. * calls a thunk. The term "thunk" here is the traditional Node.js definition:
  7. * a function that takes exactly 1 parameter, a callback.
  8. *
  9. * This function defines common behavior for all query thunks.
  10. */
  11. module.exports = function wrapThunk(fn) {
  12. return function _wrappedThunk(cb) {
  13. if (this._executionStack != null) {
  14. let str = this.toString();
  15. if (str.length > 60) {
  16. str = str.slice(0, 60) + '...';
  17. }
  18. const err = new MongooseError('Query was already executed: ' + str);
  19. err.originalStack = this._executionStack.stack;
  20. return cb(err);
  21. }
  22. this._executionStack = new Error();
  23. fn.call(this, cb);
  24. };
  25. };