blob: 23f261b9cef5eda3532103ac26fdf6fe8327c75a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
'use strict';
import noop from 'lodash/noop';
import onlyOnce from './internal/onlyOnce';
import ensureAsync from './ensureAsync';
/**
* Calls the asynchronous function `fn` with a callback parameter that allows it
* to call itself again, in series, indefinitely.
* If an error is passed to the
* callback then `errback` is called with the error, and execution stops,
* otherwise it will never be called.
*
* @name forever
* @static
* @memberOf async
* @category Control Flow
* @param {Function} fn - a function to call repeatedly. Invoked with (next).
* @param {Function} [errback] - when `fn` passes an error to it's callback,
* this function will be called, and execution stops. Invoked with (err).
* @example
*
* async.forever(
* function(next) {
* // next is suitable for passing to things that need a callback(err [, whatever]);
* // it will result in this function being called again.
* },
* function(err) {
* // if next is called with a value in its first parameter, it will appear
* // in here as 'err', and execution will stop.
* }
* );
*/
export default function forever(fn, cb) {
var done = onlyOnce(cb || noop);
var task = ensureAsync(fn);
function next(err) {
if (err) return done(err);
task(next);
}
next();
}
|