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