From cf62c0a50a9d885f2a50a2268bee78fc9007828c Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 9 Jul 2016 18:13:59 -0700 Subject: [PATCH 1/4] sorted out during's test args --- lib/doDuring.js | 30 +++++++++++++++++++++--------- lib/during.js | 20 +++++++------------- mocha_test/during.js | 7 ++++--- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/doDuring.js b/lib/doDuring.js index d12d66864..14a5ae133 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 55af1b99c..e25d90a8b 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 851acf11e..13db8b327 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); }, From 606e807cf06e2e912d0af910fcfd49af3d4461a2 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 9 Jul 2016 18:21:17 -0700 Subject: [PATCH 2/4] sort out whilst's test args --- lib/doWhilst.js | 34 +++++++++++++++++++--------------- lib/whilst.js | 2 +- mocha_test/whilst.js | 6 ++++-- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/doWhilst.js b/lib/doWhilst.js index a425999d2..163c6a9cf 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); } diff --git a/lib/whilst.js b/lib/whilst.js index 3311f4cbb..e11a14143 100644 --- a/lib/whilst.js +++ b/lib/whilst.js @@ -41,7 +41,7 @@ export default function whilst(test, iteratee, callback) { if (!test()) return callback(null); var next = rest(function(err, args) { if (err) return callback(err); - if (test.apply(this, args)) return iteratee(next); + if (test()) return iteratee(next); callback.apply(null, [null].concat(args)); }); iteratee(next); diff --git a/mocha_test/whilst.js b/mocha_test/whilst.js index a3849167f..e04c2b72f 100644 --- a/mocha_test/whilst.js +++ b/mocha_test/whilst.js @@ -8,7 +8,8 @@ describe('whilst', function(){ var count = 0; async.whilst( - function () { + function (c) { + expect(c).to.equal(undefined); call_order.push(['test', count]); return (count < 5); }, @@ -57,7 +58,8 @@ describe('whilst', function(){ count++; cb(null, count); }, - function () { + function (c) { + expect(c).to.equal(count); call_order.push(['test', count]); return (count < 5); }, From a16b450c182199a5b268662af64478060a5bfa97 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 9 Jul 2016 18:24:43 -0700 Subject: [PATCH 3/4] sorted out until's test args --- mocha_test/until.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mocha_test/until.js b/mocha_test/until.js index a73821838..e98b18403 100644 --- a/mocha_test/until.js +++ b/mocha_test/until.js @@ -7,7 +7,8 @@ describe('until', function(){ var call_order = []; var count = 0; async.until( - function () { + function (c) { + expect(c).to.equal(undefined); call_order.push(['test', count]); return (count == 5); }, @@ -42,7 +43,8 @@ describe('until', function(){ count++; cb(null, count); }, - function () { + function (c) { + expect(c).to.equal(count); call_order.push(['test', count]); return (count == 5); }, From d2764c298216d3096523d986804dd0718e2a52e2 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sat, 9 Jul 2016 18:52:17 -0700 Subject: [PATCH 4/4] wrap callbacks in onlyOnce --- lib/doDuring.js | 3 ++- lib/doWhilst.js | 4 +++- lib/during.js | 3 ++- lib/whilst.js | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/doDuring.js b/lib/doDuring.js index 14a5ae133..c536f302d 100644 --- a/lib/doDuring.js +++ b/lib/doDuring.js @@ -1,5 +1,6 @@ import noop from 'lodash/noop'; import rest from 'lodash/rest'; +import onlyOnce from './internal/onlyOnce'; /** * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in @@ -23,7 +24,7 @@ import rest from 'lodash/rest'; * will be passed an error if one occured, otherwise `null`. */ export default function doDuring(fn, test, callback) { - callback = callback || noop; + callback = onlyOnce(callback || noop); var next = rest(function(err, args) { if (err) return callback(err); diff --git a/lib/doWhilst.js b/lib/doWhilst.js index 163c6a9cf..ee8f12a9c 100644 --- a/lib/doWhilst.js +++ b/lib/doWhilst.js @@ -1,6 +1,8 @@ import noop from 'lodash/noop'; import rest from 'lodash/rest'; +import onlyOnce from './internal/onlyOnce'; + /** * The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in * the order of operations, the arguments `test` and `iteratee` are switched. @@ -25,7 +27,7 @@ import rest from 'lodash/rest'; * `iteratee`'s callback. Invoked with (err, [results]); */ export default function doWhilst(iteratee, test, callback) { - callback = callback || noop; + callback = onlyOnce(callback || noop); var next = rest(function(err, args) { if (err) return callback(err); if (test.apply(this, args)) return iteratee(next); diff --git a/lib/during.js b/lib/during.js index e25d90a8b..549ad445c 100644 --- a/lib/during.js +++ b/lib/during.js @@ -1,4 +1,5 @@ import noop from 'lodash/noop'; +import onlyOnce from './internal/onlyOnce'; /** * Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that @@ -38,7 +39,7 @@ import noop from 'lodash/noop'; * ); */ export default function during(test, fn, callback) { - callback = callback || noop; + callback = onlyOnce(callback || noop); function next(err) { if (err) return callback(err); diff --git a/lib/whilst.js b/lib/whilst.js index e11a14143..9012144e3 100644 --- a/lib/whilst.js +++ b/lib/whilst.js @@ -1,6 +1,8 @@ import noop from 'lodash/noop'; import rest from 'lodash/rest'; +import onlyOnce from './internal/onlyOnce'; + /** * Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when * stopped, or an error occurs. @@ -37,7 +39,7 @@ import rest from 'lodash/rest'; * ); */ export default function whilst(test, iteratee, callback) { - callback = callback || noop; + callback = onlyOnce(callback || noop); if (!test()) return callback(null); var next = rest(function(err, args) { if (err) return callback(err);