[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = forever;
|
---|
| 7 |
|
---|
| 8 | var _noop = require('lodash/noop');
|
---|
| 9 |
|
---|
| 10 | var _noop2 = _interopRequireDefault(_noop);
|
---|
| 11 |
|
---|
| 12 | var _onlyOnce = require('./internal/onlyOnce');
|
---|
| 13 |
|
---|
| 14 | var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
|
---|
| 15 |
|
---|
| 16 | var _ensureAsync = require('./ensureAsync');
|
---|
| 17 |
|
---|
| 18 | var _ensureAsync2 = _interopRequireDefault(_ensureAsync);
|
---|
| 19 |
|
---|
| 20 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
| 21 |
|
---|
| 22 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
| 23 |
|
---|
| 24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Calls the asynchronous function `fn` with a callback parameter that allows it
|
---|
| 28 | * to call itself again, in series, indefinitely.
|
---|
| 29 |
|
---|
| 30 | * If an error is passed to the callback then `errback` is called with the
|
---|
| 31 | * error, and execution stops, otherwise it will never be called.
|
---|
| 32 | *
|
---|
| 33 | * @name forever
|
---|
| 34 | * @static
|
---|
| 35 | * @memberOf module:ControlFlow
|
---|
| 36 | * @method
|
---|
| 37 | * @category Control Flow
|
---|
| 38 | * @param {AsyncFunction} fn - an async function to call repeatedly.
|
---|
| 39 | * Invoked with (next).
|
---|
| 40 | * @param {Function} [errback] - when `fn` passes an error to it's callback,
|
---|
| 41 | * this function will be called, and execution stops. Invoked with (err).
|
---|
| 42 | * @example
|
---|
| 43 | *
|
---|
| 44 | * async.forever(
|
---|
| 45 | * function(next) {
|
---|
| 46 | * // next is suitable for passing to things that need a callback(err [, whatever]);
|
---|
| 47 | * // it will result in this function being called again.
|
---|
| 48 | * },
|
---|
| 49 | * function(err) {
|
---|
| 50 | * // if next is called with a value in its first parameter, it will appear
|
---|
| 51 | * // in here as 'err', and execution will stop.
|
---|
| 52 | * }
|
---|
| 53 | * );
|
---|
| 54 | */
|
---|
| 55 | function forever(fn, errback) {
|
---|
| 56 | var done = (0, _onlyOnce2.default)(errback || _noop2.default);
|
---|
| 57 | var task = (0, _wrapAsync2.default)((0, _ensureAsync2.default)(fn));
|
---|
| 58 |
|
---|
| 59 | function next(err) {
|
---|
| 60 | if (err) return done(err);
|
---|
| 61 | task(next);
|
---|
| 62 | }
|
---|
| 63 | next();
|
---|
| 64 | }
|
---|
| 65 | module.exports = exports['default']; |
---|