Skip to content

Commit

Permalink
use ensureAsync with forever. Fixes #622
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Early committed May 25, 2015
1 parent aba3e0c commit 7c7326b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
9 changes: 4 additions & 5 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
26 changes: 24 additions & 2 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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) {
Expand Down

0 comments on commit 7c7326b

Please sign in to comment.