UNPKG

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