doWhilst.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 _rest = require('lodash/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 [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
  15. * the order of operations, the arguments `test` and `iteratee` are switched.
  16. *
  17. * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
  18. *
  19. * @name doWhilst
  20. * @static
  21. * @memberOf module:ControlFlow
  22. * @method
  23. * @see [async.whilst]{@link module:ControlFlow.whilst}
  24. * @category Control Flow
  25. * @param {Function} iteratee - A function which is called each time `test`
  26. * passes. The function is passed a `callback(err)`, which must be called once
  27. * it has completed with an optional `err` argument. Invoked with (callback).
  28. * @param {Function} test - synchronous truth test to perform after each
  29. * execution of `iteratee`. Invoked with Invoked with the non-error callback
  30. * results of `iteratee`.
  31. * @param {Function} [callback] - A callback which is called after the test
  32. * function has failed and repeated execution of `iteratee` has stopped.
  33. * `callback` will be passed an error and any arguments passed to the final
  34. * `iteratee`'s callback. Invoked with (err, [results]);
  35. */
  36. function doWhilst(iteratee, test, callback) {
  37. callback = (0, _onlyOnce2.default)(callback || _noop2.default);
  38. var next = (0, _rest2.default)(function (err, args) {
  39. if (err) return callback(err);
  40. if (test.apply(this, args)) return iteratee(next);
  41. callback.apply(null, [null].concat(args));
  42. });
  43. iteratee(next);
  44. }
  45. module.exports = exports['default'];