diff options
author | Alexander Early <alexander.early@gmail.com> | 2016-07-09 18:21:17 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2016-07-09 18:21:17 -0700 |
commit | 606e807cf06e2e912d0af910fcfd49af3d4461a2 (patch) | |
tree | 8419cd10baf2cf88e93c217390d88d284b9c7e97 /lib/doWhilst.js | |
parent | cf62c0a50a9d885f2a50a2268bee78fc9007828c (diff) | |
download | async-606e807cf06e2e912d0af910fcfd49af3d4461a2.tar.gz |
sort out whilst's test args
Diffstat (limited to 'lib/doWhilst.js')
-rw-r--r-- | lib/doWhilst.js | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/lib/doWhilst.js b/lib/doWhilst.js index a425999..163c6a9 100644 --- a/lib/doWhilst.js +++ b/lib/doWhilst.js @@ -1,8 +1,9 @@ -import whilst from './whilst'; +import noop from 'lodash/noop'; +import rest from 'lodash/rest'; /** * The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in - * the order of operations, the arguments `test` and `fn` are switched. + * the order of operations, the arguments `test` and `iteratee` are switched. * * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript. * @@ -12,20 +13,23 @@ import whilst from './whilst'; * @method * @see [async.whilst]{@link module:ControlFlow.whilst} * @category Control Flow - * @param {Function} fn - A function which is called each time `test` passes. - * The function is passed a `callback(err)`, which must be called once it has - * completed with an optional `err` argument. Invoked with (callback). + * @param {Function} iteratee - A function which is called each time `test` + * passes. The function is passed a `callback(err)`, which must be called once + * it has completed with an optional `err` argument. Invoked with (callback). * @param {Function} test - synchronous truth test to perform after each - * execution of `fn`. Invoked with Invoked with the non-error callback results - * of `fn`. + * execution of `iteratee`. Invoked with Invoked with the non-error callback + * results of `iteratee`. * @param {Function} [callback] - A callback which is called after the test - * function has failed and repeated execution of `fn` has stopped. `callback` - * will be passed an error and any arguments passed to the final `fn`'s - * callback. Invoked with (err, [results]); + * 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]); */ -export default function doWhilst(fn, test, callback) { - var calls = 0; - whilst(function() { - return ++calls <= 1 || test.apply(this, arguments); - }, fn, callback); +export default function doWhilst(iteratee, test, callback) { + callback = callback || noop; + var next = rest(function(err, args) { + if (err) return callback(err); + if (test.apply(this, args)) return iteratee(next); + callback.apply(null, [null].concat(args)); + }); + iteratee(next); } |