diff options
author | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-15 12:48:13 +0000 |
---|---|---|
committer | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-15 12:48:13 +0000 |
commit | cce102df20916956f5ccad82b7da6019387e2fdc (patch) | |
tree | 9ffda1f639b73ce21879506e085d158f0065cde2 | |
parent | a0d565d01bd256ea3d67104925139576d110aadb (diff) | |
download | async-cce102df20916956f5ccad82b7da6019387e2fdc.tar.gz |
added forever
-rw-r--r-- | README.md | 9 | ||||
-rwxr-xr-x | lib/async.js | 13 | ||||
-rwxr-xr-x | test/test-async.js | 18 |
3 files changed, 40 insertions, 0 deletions
@@ -91,6 +91,7 @@ So far its been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage: * [doWhilst](#doWhilst) * [until](#until) * [doUntil](#doUntil) +* [forever](#forever) * [waterfall](#waterfall) * [compose](#compose) * [applyEach](#applyEach) @@ -754,6 +755,14 @@ The inverse of async.whilst. Like doWhilst except the test is inverted. Note the argument ordering differs from `until`. +--------------------------------------- + +<a name="forever" /> +### forever(fn, callback) + +Calls the asynchronous function 'fn' repeatedly, in series, indefinitely. +If an error is passed to fn's callback then 'callback' is called with the +error, otherwise it will never be called. --------------------------------------- diff --git a/lib/async.js b/lib/async.js index 98f2127..b5bfc4c 100755 --- a/lib/async.js +++ b/lib/async.js @@ -910,6 +910,19 @@ } }; + async.forever = function (fn, callback) { + function next(err) { + if (err) { + if (callback) { + return callback(err); + } + throw err; + } + fn(next); + } + next(); + }; + // AMD / RequireJS if (typeof define !== 'undefined' && define.amd) { define([], function () { diff --git a/test/test-async.js b/test/test-async.js index cba6f4b..f9b9c08 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -66,6 +66,24 @@ function getFunctionsObject(call_order) { }; } +exports['forever'] = function (test) { + test.expect(1); + var counter = 0; + function addOne(callback) { + counter++; + if (counter === 50) { + return callback('too big!'); + } + async.setImmediate(function () { + callback(); + }); + } + async.forever(addOne, function (err) { + test.equal(err, 'too big!'); + test.done(); + }); +}; + exports['applyEach'] = function (test) { test.expect(4); var call_order = []; |