forever.js 1.8 KB

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