diff options
Diffstat (limited to 'lib/doWhilst.js')
-rw-r--r-- | lib/doWhilst.js | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/doWhilst.js b/lib/doWhilst.js index 324d2e0..b3d52a5 100644 --- a/lib/doWhilst.js +++ b/lib/doWhilst.js @@ -17,22 +17,34 @@ import wrapAsync from './internal/wrapAsync'; * @category Control Flow * @param {AsyncFunction} iteratee - A function which is called each time `test` * passes. Invoked with (callback). - * @param {Function} test - synchronous truth test to perform after each - * execution of `iteratee`. Invoked with any non-error callback results of - * `iteratee`. + * @param {AsyncFunction} test - asynchronous truth test to perform after each + * execution of `iteratee`. Invoked with (...args, callback), where `...args` are the + * non-error args from the previous callback of `iteratee`. * @param {Function} [callback] - A callback which is called after the test * function has failed and repeated execution of `iteratee` has stopped. * `callback` will be passed an error and any arguments passed to the final * `iteratee`'s callback. Invoked with (err, [results]); + * @return undefined */ export default function doWhilst(iteratee, test, callback) { callback = onlyOnce(callback || noop); - var _iteratee = wrapAsync(iteratee); - function next (err, ...args) { + var _fn = wrapAsync(iteratee); + var _test = wrapAsync(test); + var results + + function next(err, ...args) { + if (err) return callback(err); + if (err === false) return; + results = args + _test(...args, check); + } + + function check(err, truth) { if (err) return callback(err); if (err === false) return; - if (test(...args)) return _iteratee(next); - callback(null, ...args); + if (!truth) return callback(null, ...results); + _fn(next); } - _iteratee(next); + + return check(null, true); } |