summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alex@npmjs.com>2018-06-10 18:39:45 -0700
committerAlexander Early <alex@npmjs.com>2018-06-10 18:39:45 -0700
commit062908b448f3de98fe89aebf27b404f58e116a60 (patch)
treea35bf05c68d40861757af6a4eb09799ddb57c85d
parentdfd21d0e38f5f0e25bd9ccde420782f7d5faf226 (diff)
downloadasync-062908b448f3de98fe89aebf27b404f58e116a60.tar.gz
cancelable whilst/until/during/forever
-rw-r--r--lib/doDuring.js2
-rw-r--r--lib/doWhilst.js1
-rw-r--r--lib/during.js2
-rw-r--r--lib/forever.js1
-rw-r--r--lib/whilst.js1
-rw-r--r--test/during.js48
-rw-r--r--test/forever.js13
-rw-r--r--test/until.js32
-rw-r--r--test/whilst.js32
9 files changed, 132 insertions, 0 deletions
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)
+ })
});