until.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = until;
  6. var _whilst = require('./whilst.js');
  7. var _whilst2 = _interopRequireDefault(_whilst);
  8. var _wrapAsync = require('./internal/wrapAsync.js');
  9. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Repeatedly call `iteratee` until `test` returns `true`. Calls `callback` when
  13. * stopped, or an error occurs. `callback` will be passed an error and any
  14. * arguments passed to the final `iteratee`'s callback.
  15. *
  16. * The inverse of [whilst]{@link module:ControlFlow.whilst}.
  17. *
  18. * @name until
  19. * @static
  20. * @memberOf module:ControlFlow
  21. * @method
  22. * @see [async.whilst]{@link module:ControlFlow.whilst}
  23. * @category Control Flow
  24. * @param {AsyncFunction} test - asynchronous truth test to perform before each
  25. * execution of `iteratee`. Invoked with (callback).
  26. * @param {AsyncFunction} iteratee - An async function which is called each time
  27. * `test` fails. Invoked with (callback).
  28. * @param {Function} [callback] - A callback which is called after the test
  29. * function has passed and repeated execution of `iteratee` has stopped. `callback`
  30. * will be passed an error and any arguments passed to the final `iteratee`'s
  31. * callback. Invoked with (err, [results]);
  32. * @returns {Promise} a promise, if a callback is not passed
  33. *
  34. * @example
  35. * const results = []
  36. * let finished = false
  37. * async.until(function test(cb) {
  38. * cb(null, finished)
  39. * }, function iter(next) {
  40. * fetchPage(url, (err, body) => {
  41. * if (err) return next(err)
  42. * results = results.concat(body.objects)
  43. * finished = !!body.next
  44. * next(err)
  45. * })
  46. * }, function done (err) {
  47. * // all pages have been fetched
  48. * })
  49. */
  50. function until(test, iteratee, callback) {
  51. const _test = (0, _wrapAsync2.default)(test);
  52. return (0, _whilst2.default)(cb => _test((err, truth) => cb(err, !truth)), iteratee, callback);
  53. }
  54. module.exports = exports['default'];