From 062908b448f3de98fe89aebf27b404f58e116a60 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sun, 10 Jun 2018 18:39:45 -0700 Subject: cancelable whilst/until/during/forever --- lib/doDuring.js | 2 ++ lib/doWhilst.js | 1 + lib/during.js | 2 ++ lib/forever.js | 1 + lib/whilst.js | 1 + test/during.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ test/forever.js | 13 +++++++++++++ test/until.js | 32 ++++++++++++++++++++++++++++++++ test/whilst.js | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 132 insertions(+) diff --git a/lib/doDuring.js b/lib/doDuring.js index ac2a04c..5eb2b92 100644 --- a/lib/doDuring.js +++ b/lib/doDuring.js @@ -30,6 +30,7 @@ export default function doDuring(fn, test, callback) { function next(err/*, ...args*/) { if (err) return callback(err); + if (err === false) return; var args = slice(arguments, 1); args.push(check); _test.apply(this, args); @@ -37,6 +38,7 @@ export default function doDuring(fn, test, callback) { function check(err, truth) { if (err) return callback(err); + if (err === false) return; if (!truth) return callback(null); _fn(next); } diff --git a/lib/doWhilst.js b/lib/doWhilst.js index 3c2b865..5245820 100644 --- a/lib/doWhilst.js +++ b/lib/doWhilst.js @@ -31,6 +31,7 @@ export default function doWhilst(iteratee, test, callback) { var _iteratee = wrapAsync(iteratee); var next = function(err/*, ...args*/) { if (err) return callback(err); + if (err === false) return; var args = slice(arguments, 1); if (test.apply(this, args)) return _iteratee(next); callback.apply(null, [null].concat(args)); diff --git a/lib/during.js b/lib/during.js index cda9979..02da045 100644 --- a/lib/during.js +++ b/lib/during.js @@ -45,11 +45,13 @@ export default function during(test, fn, callback) { function next(err) { if (err) return callback(err); + if (err === false) return; _test(check); } function check(err, truth) { if (err) return callback(err); + if (err === false) return; if (!truth) return callback(null); _fn(next); } diff --git a/lib/forever.js b/lib/forever.js index 3753251..fb69adf 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -38,6 +38,7 @@ export default function forever(fn, errback) { function next(err) { if (err) return done(err); + if (err === false) return; task(next); } next(); diff --git a/lib/whilst.js b/lib/whilst.js index da748fa..32c3b52 100644 --- a/lib/whilst.js +++ b/lib/whilst.js @@ -44,6 +44,7 @@ export default function whilst(test, iteratee, callback) { if (!test()) return callback(null); var next = function(err/*, ...args*/) { if (err) return callback(err); + if (err === false) return; if (test()) return _iteratee(next); var args = slice(arguments, 1); callback.apply(null, [null].concat(args)); diff --git a/test/during.js b/test/during.js index 13db8b3..e0636e5 100644 --- a/test/during.js +++ b/test/during.js @@ -34,6 +34,22 @@ describe('during', function() { ); }); + it('during canceling', (done) => { + let counter = 0; + async.during( + cb => cb(null, true), + cb => { + counter++ + cb(counter === 2 ? false : null); + }, + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) + it('doDuring', function(done) { var call_order = []; @@ -95,4 +111,36 @@ describe('during', function() { } ); }); + + it('doDuring canceling', (done) => { + let counter = 0; + async.doDuring( + cb => { + counter++ + cb(counter === 2 ? false : null); + }, + cb => cb(null, true), + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) + + it('doDuring canceling in test', (done) => { + let counter = 0; + async.doDuring( + cb => { + counter++ + cb(null, counter); + }, + (n, cb) => cb(n === 2 ? false : null, true), + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) }); diff --git a/test/forever.js b/test/forever.js index e00a22d..b0a75ec 100644 --- a/test/forever.js +++ b/test/forever.js @@ -38,5 +38,18 @@ describe('forever', function(){ done(); }); }); + + it('should cancel', (done) => { + var counter = 0; + async.forever(cb => { + counter++ + cb(counter === 2 ? false : null) + }, () => { throw new Error('should not get here') }) + + setTimeout(() => { + expect(counter).to.eql(2) + done() + }) + }) }); }); diff --git a/test/until.js b/test/until.js index e98b184..2b82fe1 100644 --- a/test/until.js +++ b/test/until.js @@ -34,6 +34,22 @@ describe('until', function(){ ); }); + it('until canceling', (done) => { + let counter = 0; + async.until( + () => false, + cb => { + counter++ + cb(counter === 2 ? false: null); + }, + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) + it('doUntil', function(done) { var call_order = []; var count = 0; @@ -92,4 +108,20 @@ describe('until', function(){ } ); }); + + it('doUntil canceling', (done) => { + let counter = 0; + async.doUntil( + cb => { + counter++ + cb(counter === 2 ? false: null); + }, + () => false, + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) }); diff --git a/test/whilst.js b/test/whilst.js index e04c2b7..2ce2f23 100644 --- a/test/whilst.js +++ b/test/whilst.js @@ -48,6 +48,22 @@ describe('whilst', function(){ done(); }); + it('whilst canceling', function(done) { + var counter = 0; + async.whilst( + function () { return counter < 3; }, + function (cb) { + counter++; + cb(counter === 2 ? false : null); + }, + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }); + it('doWhilst', function(done) { var call_order = []; @@ -122,4 +138,20 @@ describe('whilst', function(){ } ); }); + + it('doWhilst canceling', (done) => { + let counter = 0; + async.doWhilst( + cb => { + counter++ + cb(counter === 2 ? false : null); + }, + () => true, + () => { throw new Error('should not get here')} + ); + setTimeout(() => { + expect(counter).to.equal(2); + done(); + }, 10) + }) }); -- cgit v1.2.1