forever.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. var _wrapAsync = require('./internal/wrapAsync');
  13. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * Calls the asynchronous function `fn` with a callback parameter that allows it
  17. * to call itself again, in series, indefinitely.
  18. * If an error is passed to the callback then `errback` is called with the
  19. * error, and execution stops, otherwise it will never be called.
  20. *
  21. * @name forever
  22. * @static
  23. * @memberOf module:ControlFlow
  24. * @method
  25. * @category Control Flow
  26. * @param {AsyncFunction} fn - an async function to call repeatedly.
  27. * Invoked with (next).
  28. * @param {Function} [errback] - when `fn` passes an error to it's callback,
  29. * this function will be called, and execution stops. Invoked with (err).
  30. * @example
  31. *
  32. * async.forever(
  33. * function(next) {
  34. * // next is suitable for passing to things that need a callback(err [, whatever]);
  35. * // it will result in this function being called again.
  36. * },
  37. * function(err) {
  38. * // if next is called with a value in its first parameter, it will appear
  39. * // in here as 'err', and execution will stop.
  40. * }
  41. * );
  42. */
  43. function forever(fn, errback) {
  44. var done = (0, _onlyOnce2.default)(errback || _noop2.default);
  45. var task = (0, _wrapAsync2.default)((0, _ensureAsync2.default)(fn));
  46. function next(err) {
  47. if (err) return done(err);
  48. task(next);
  49. }
  50. next();
  51. }
  52. module.exports = exports['default'];