diff options
author | Alexander Early <alexander.early@gmail.com> | 2016-07-09 18:13:59 -0700 |
---|---|---|
committer | Alexander Early <alexander.early@gmail.com> | 2016-07-09 18:13:59 -0700 |
commit | cf62c0a50a9d885f2a50a2268bee78fc9007828c (patch) | |
tree | 1ae8910e70a11d9539d0d4d6f6774483482eb878 | |
parent | ca22b96ec512699dee73d252ec4f557d4cf9c31d (diff) | |
download | async-cf62c0a50a9d885f2a50a2268bee78fc9007828c.tar.gz |
sorted out during's test args
-rw-r--r-- | lib/doDuring.js | 30 | ||||
-rw-r--r-- | lib/during.js | 20 | ||||
-rw-r--r-- | mocha_test/during.js | 7 |
3 files changed, 32 insertions, 25 deletions
diff --git a/lib/doDuring.js b/lib/doDuring.js index d12d668..14a5ae1 100644 --- a/lib/doDuring.js +++ b/lib/doDuring.js @@ -1,4 +1,5 @@ -import during from './during'; +import noop from 'lodash/noop'; +import rest from 'lodash/rest'; /** * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in @@ -15,17 +16,28 @@ import during from './during'; * 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 - asynchronous truth test to perform before each - * execution of `fn`. Invoked with (callback). + * execution of `fn`. Invoked with (...args, callback), where `...args` are the + * non-error args from the previous callback of `fn`. * @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]); + * will be passed an error if one occured, otherwise `null`. */ export default function doDuring(fn, test, callback) { - var calls = 0; + callback = callback || noop; + + var next = rest(function(err, args) { + if (err) return callback(err); + args.push(check); + test.apply(this, args); + }); + + function check(err, truth) { + if (err) return callback(err); + if (!truth) return callback(null); + fn(next); + } + + check(null, true); - during(function(next) { - if (calls++ < 1) return next(null, true); - test.apply(this, arguments); - }, fn, callback); } + diff --git a/lib/during.js b/lib/during.js index 55af1b9..e25d90a 100644 --- a/lib/during.js +++ b/lib/during.js @@ -1,5 +1,4 @@ import noop from 'lodash/noop'; -import rest from 'lodash/rest'; /** * Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that @@ -20,8 +19,7 @@ import rest from 'lodash/rest'; * completed with an optional `err` argument. Invoked with (callback). * @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]); + * will be passed an error, if one occured, otherwise `null`. * @example * * var count = 0; @@ -42,20 +40,16 @@ import rest from 'lodash/rest'; export default function during(test, fn, callback) { callback = callback || noop; - var next = rest(function(err, args) { - if (err) { - callback(err); - } else { - args.push(check); - test.apply(this, args); - } - }); + function next(err) { + if (err) return callback(err); + test(check); + } - var check = function(err, truth) { + function check(err, truth) { if (err) return callback(err); if (!truth) return callback(null); fn(next); - }; + } test(check); } diff --git a/mocha_test/during.js b/mocha_test/during.js index 851acf1..13db8b3 100644 --- a/mocha_test/during.js +++ b/mocha_test/during.js @@ -16,7 +16,7 @@ describe('during', function() { function (cb) { call_order.push(['iteratee', count]); count++; - cb(); + cb(null, count); }, function (err) { assert(err === null, err + " passed instead of 'null'"); @@ -42,9 +42,10 @@ describe('during', function() { function (cb) { call_order.push(['iteratee', count]); count++; - cb(); + cb(null, count); }, - function (cb) { + function (c, cb) { + expect(c).to.equal(count); call_order.push(['test', count]); cb(null, count < 5); }, |