summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <aearly@fluid.com>2015-05-25 15:08:00 -0700
committerAlexander Early <aearly@fluid.com>2015-05-25 15:08:00 -0700
commit7c7326b3892774641b4258f7207842ee8846fdd2 (patch)
tree5054eac2b470d74b766f30765d6efae7341b7232
parentaba3e0c1af87f0f89bbddb6d3a2c56ffb9176c9b (diff)
downloadasync-ensure_async.tar.gz
use ensureAsync with forever. Fixes #622ensure_async
-rw-r--r--lib/async.js9
-rwxr-xr-xtest/test-async.js26
2 files changed, 28 insertions, 7 deletions
diff --git a/lib/async.js b/lib/async.js
index 06ede14..b8c001b 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -1255,14 +1255,13 @@
async.applyEachSeries = doSeries(_applyEach);
async.forever = function (fn, callback) {
+ var done = only_once(callback || noop);
+ var task = ensureAsync(fn);
function next(err) {
if (err) {
- if (callback) {
- return callback(err);
- }
- throw err;
+ return done(err);
}
- fn(next);
+ task(next);
}
next();
};
diff --git a/test/test-async.js b/test/test-async.js
index 6797afe..eba90e9 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -80,8 +80,10 @@ function getFunctionsObject(call_order) {
};
}
-exports['forever'] = function (test) {
- test.expect(1);
+exports['forever'] = {
+
+'async': function (test) {
+ test.expect(2);
var counter = 0;
function addOne(callback) {
counter++;
@@ -94,8 +96,28 @@ exports['forever'] = function (test) {
}
async.forever(addOne, function (err) {
test.equal(err, 'too big!');
+ test.equal(counter, 50);
test.done();
});
+},
+
+'sync': function (test) {
+ test.expect(2);
+ var counter = 0;
+ function addOne(callback) {
+ counter++;
+ if (counter === 50000) {
+ return callback('too big!');
+ }
+ callback();
+ }
+ async.forever(addOne, function (err) {
+ test.equal(err, 'too big!');
+ test.equal(counter, 50000);
+ test.done();
+ });
+}
+
};
exports['applyEach'] = function (test) {