doWhilst.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = doWhilst;
  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 [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
  17. * the order of operations, the arguments `test` and `iteratee` are switched.
  18. *
  19. * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
  20. *
  21. * @name doWhilst
  22. * @static
  23. * @memberOf module:ControlFlow
  24. * @method
  25. * @see [async.whilst]{@link module:ControlFlow.whilst}
  26. * @category Control Flow
  27. * @param {AsyncFunction} iteratee - A function which is called each time `test`
  28. * passes. Invoked with (callback).
  29. * @param {Function} test - synchronous truth test to perform after each
  30. * execution of `iteratee`. Invoked with any non-error callback results of
  31. * `iteratee`.
  32. * @param {Function} [callback] - A callback which is called after the test
  33. * function has failed and repeated execution of `iteratee` has stopped.
  34. * `callback` will be passed an error and any arguments passed to the final
  35. * `iteratee`'s callback. Invoked with (err, [results]);
  36. */
  37. function doWhilst(iteratee, test, callback) {
  38. callback = (0, _onlyOnce2.default)(callback || _noop2.default);
  39. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  40. var next = function (err /*, ...args*/) {
  41. if (err) return callback(err);
  42. var args = (0, _slice2.default)(arguments, 1);
  43. if (test.apply(this, args)) return _iteratee(next);
  44. callback.apply(null, [null].concat(args));
  45. };
  46. _iteratee(next);
  47. }
  48. module.exports = exports['default'];