doDuring.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 _rest = require('./internal/rest');
  9. var _rest2 = _interopRequireDefault(_rest);
  10. var _onlyOnce = require('./internal/onlyOnce');
  11. var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
  15. * the order of operations, the arguments `test` and `fn` are switched.
  16. *
  17. * Also a version of [`doWhilst`]{@link module:ControlFlow.doWhilst} with asynchronous `test` function.
  18. * @name doDuring
  19. * @static
  20. * @memberOf module:ControlFlow
  21. * @method
  22. * @see [async.during]{@link module:ControlFlow.during}
  23. * @category Control Flow
  24. * @param {Function} fn - A function which is called each time `test` passes.
  25. * The function is passed a `callback(err)`, which must be called once it has
  26. * completed with an optional `err` argument. Invoked with (callback).
  27. * @param {Function} test - asynchronous truth test to perform before each
  28. * execution of `fn`. Invoked with (...args, callback), where `...args` are the
  29. * non-error args from the previous callback of `fn`.
  30. * @param {Function} [callback] - A callback which is called after the test
  31. * function has failed and repeated execution of `fn` has stopped. `callback`
  32. * will be passed an error if one occured, otherwise `null`.
  33. */
  34. function doDuring(fn, test, callback) {
  35. callback = (0, _onlyOnce2.default)(callback || _noop2.default);
  36. var next = (0, _rest2.default)(function (err, args) {
  37. if (err) return callback(err);
  38. args.push(check);
  39. test.apply(this, args);
  40. });
  41. function check(err, truth) {
  42. if (err) return callback(err);
  43. if (!truth) return callback(null);
  44. fn(next);
  45. }
  46. check(null, true);
  47. }
  48. module.exports = exports['default'];