doDuring.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = doDuring;
  6. var _noop = require('lodash/noop');
  7. var _noop2 = _interopRequireDefault(_noop);
  8. var _slice = require('./internal/slice');
  9. var _slice2 = _interopRequireDefault(_slice);
  10. var _onlyOnce = require('./internal/onlyOnce');
  11. var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
  12. var _wrapAsync = require('./internal/wrapAsync');
  13. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
  17. * the order of operations, the arguments `test` and `fn` are switched.
  18. *
  19. * Also a version of [`doWhilst`]{@link module:ControlFlow.doWhilst} with asynchronous `test` function.
  20. * @name doDuring
  21. * @static
  22. * @memberOf module:ControlFlow
  23. * @method
  24. * @see [async.during]{@link module:ControlFlow.during}
  25. * @category Control Flow
  26. * @param {AsyncFunction} fn - An async function which is called each time
  27. * `test` passes. Invoked with (callback).
  28. * @param {AsyncFunction} test - asynchronous truth test to perform before each
  29. * execution of `fn`. Invoked with (...args, callback), where `...args` are the
  30. * non-error args from the previous callback of `fn`.
  31. * @param {Function} [callback] - A callback which is called after the test
  32. * function has failed and repeated execution of `fn` has stopped. `callback`
  33. * will be passed an error if one occurred, otherwise `null`.
  34. */
  35. function doDuring(fn, test, callback) {
  36. callback = (0, _onlyOnce2.default)(callback || _noop2.default);
  37. var _fn = (0, _wrapAsync2.default)(fn);
  38. var _test = (0, _wrapAsync2.default)(test);
  39. function next(err /*, ...args*/) {
  40. if (err) return callback(err);
  41. var args = (0, _slice2.default)(arguments, 1);
  42. args.push(check);
  43. _test.apply(this, args);
  44. };
  45. function check(err, truth) {
  46. if (err) return callback(err);
  47. if (!truth) return callback(null);
  48. _fn(next);
  49. }
  50. check(null, true);
  51. }
  52. module.exports = exports['default'];