Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: remove during, make test functions in until/whilst async #1557

Merged
merged 3 commits into from Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 0 additions & 46 deletions lib/doDuring.js

This file was deleted.

13 changes: 9 additions & 4 deletions lib/doUntil.js
@@ -1,4 +1,5 @@
import doWhilst from './doWhilst';
import wrapAsync from './internal/wrapAsync';

/**
* Like ['doWhilst']{@link module:ControlFlow.doWhilst}, except the `test` is inverted. Note the
Expand All @@ -12,14 +13,18 @@ import doWhilst from './doWhilst';
* @category Control Flow
* @param {AsyncFunction} iteratee - An async function which is called each time
* `test` fails. 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 {Function} test - asynchronous truth test to perform after each
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Sorry, same as before, I think the param type is AsyncFunction.

* execution of `iteratee`. 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 passed 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 doUntil(iteratee, test, callback) {
doWhilst(iteratee, (...args) => !test(...args), callback);
const _test = wrapAsync(test)
doWhilst(iteratee, (...args) => {
const cb = args.pop()
_test(...args, (err, truth) => cb (err, !truth))
}, callback);
}
28 changes: 20 additions & 8 deletions lib/doWhilst.js
Expand Up @@ -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 {Function} 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 `fn`.
* @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);
}
60 changes: 0 additions & 60 deletions lib/during.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/index.js
Expand Up @@ -79,10 +79,8 @@ import detect from './detect'
import detectLimit from './detectLimit'
import detectSeries from './detectSeries'
import dir from './dir'
import doDuring from './doDuring'
import doUntil from './doUntil'
import doWhilst from './doWhilst'
import during from './during'
import each from './each'
import eachLimit from './eachLimit'
import eachOf from './eachOf'
Expand Down Expand Up @@ -158,10 +156,8 @@ export default {
detectLimit,
detectSeries,
dir,
doDuring,
doUntil,
doWhilst,
during,
each,
eachLimit,
eachOf,
Expand Down Expand Up @@ -262,10 +258,8 @@ export {
detectLimit as detectLimit,
detectSeries as detectSeries,
dir as dir,
doDuring as doDuring,
doUntil as doUntil,
doWhilst as doWhilst,
during as during,
each as each,
eachLimit as eachLimit,
eachOf as eachOf,
Expand Down
22 changes: 19 additions & 3 deletions lib/until.js
@@ -1,4 +1,5 @@
import whilst from './whilst';
import wrapAsync from './internal/wrapAsync';

/**
* Repeatedly call `iteratee` until `test` returns `true`. Calls `callback` when
Expand All @@ -13,15 +14,30 @@ import whilst from './whilst';
* @method
* @see [async.whilst]{@link module:ControlFlow.whilst}
* @category Control Flow
* @param {Function} test - synchronous truth test to perform before each
* execution of `iteratee`. Invoked with ().
* @param {Function} test - asynchronous truth test to perform before each
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think the parameter type is AsyncFunction.

* execution of `iteratee`. Invoked with (callback).
* @param {AsyncFunction} iteratee - An async function which is called each time
* `test` fails. Invoked with (callback).
* @param {Function} [callback] - A callback which is called after the test
* function has passed 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]);
*
* @example
* const results = []
* async.until(function iter(next) {
* fetchPage(url, (err, body) => {
* if (err) return next(err)
* results = results.concat(body.objects)
* next(err, body)
* })
* }, function test(page, cb) {
* cb(null, page.next == null)
* }, function done (err) {
* // all pages have been fetched
* })
*/
export default function until(test, iteratee, callback) {
whilst((...args) => !test(...args), iteratee, callback);
const _test = wrapAsync(test)
whilst((cb) => _test((err, truth) => cb (err, !truth)), iteratee, callback);
}
30 changes: 20 additions & 10 deletions lib/whilst.js
Expand Up @@ -12,7 +12,7 @@ import wrapAsync from './internal/wrapAsync';
* @memberOf module:ControlFlow
* @method
* @category Control Flow
* @param {Function} test - synchronous truth test to perform before each
* @param {AsyncFunction} test - asynchronous truth test to perform before each
* execution of `iteratee`. Invoked with ().
* @param {AsyncFunction} iteratee - An async function which is called each time
* `test` passes. Invoked with (callback).
Expand All @@ -25,8 +25,8 @@ import wrapAsync from './internal/wrapAsync';
*
* var count = 0;
* async.whilst(
* function() { return count < 5; },
* function(callback) {
* function test(cb) { cb(null, count < 5;) },
* function iter(callback) {
* count++;
* setTimeout(function() {
* callback(null, count);
Expand All @@ -39,13 +39,23 @@ import wrapAsync from './internal/wrapAsync';
*/
export default function whilst(test, iteratee, callback) {
callback = onlyOnce(callback || noop);
var _iteratee = wrapAsync(iteratee);
if (!test()) return callback(null);
var next = (err, ...args) => {
var _fn = wrapAsync(iteratee);
var _test = wrapAsync(test);
var results

function next(err, ...rest) {
if (err) return callback(err);
results = rest;
if (err === false) return;
if (test()) return _iteratee(next);
callback(null, ...args);
};
_iteratee(next);
_test(check);
}

function check(err, truth) {
if (err) return callback(err);
if (err === false) return;
if (!truth) return callback(null, ...results);
_fn(next);
}

return _test(check);
}