1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = timeout;
  6. var _initialParams = require('./internal/initialParams');
  7. var _initialParams2 = _interopRequireDefault(_initialParams);
  8. var _wrapAsync = require('./internal/wrapAsync');
  9. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Sets a time limit on an asynchronous function. If the function does not call
  13. * its callback within the specified milliseconds, it will be called with a
  14. * timeout error. The code property for the error object will be `'ETIMEDOUT'`.
  15. *
  16. * @name timeout
  17. * @static
  18. * @memberOf module:Utils
  19. * @method
  20. * @category Util
  21. * @param {AsyncFunction} asyncFn - The async function to limit in time.
  22. * @param {number} milliseconds - The specified time limit.
  23. * @param {*} [info] - Any variable you want attached (`string`, `object`, etc)
  24. * to timeout Error for more information..
  25. * @returns {AsyncFunction} Returns a wrapped function that can be used with any
  26. * of the control flow functions.
  27. * Invoke this function with the same parameters as you would `asyncFunc`.
  28. * @example
  29. *
  30. * function myFunction(foo, callback) {
  31. * doAsyncTask(foo, function(err, data) {
  32. * // handle errors
  33. * if (err) return callback(err);
  34. *
  35. * // do some stuff ...
  36. *
  37. * // return processed data
  38. * return callback(null, data);
  39. * });
  40. * }
  41. *
  42. * var wrapped = async.timeout(myFunction, 1000);
  43. *
  44. * // call `wrapped` as you would `myFunction`
  45. * wrapped({ bar: 'bar' }, function(err, data) {
  46. * // if `myFunction` takes < 1000 ms to execute, `err`
  47. * // and `data` will have their expected values
  48. *
  49. * // else `err` will be an Error with the code 'ETIMEDOUT'
  50. * });
  51. */
  52. function timeout(asyncFn, milliseconds, info) {
  53. var fn = (0, _wrapAsync2.default)(asyncFn);
  54. return (0, _initialParams2.default)(function (args, callback) {
  55. var timedOut = false;
  56. var timer;
  57. function timeoutCallback() {
  58. var name = asyncFn.name || 'anonymous';
  59. var error = new Error('Callback function "' + name + '" timed out.');
  60. error.code = 'ETIMEDOUT';
  61. if (info) {
  62. error.info = info;
  63. }
  64. timedOut = true;
  65. callback(error);
  66. }
  67. args.push(function () {
  68. if (!timedOut) {
  69. callback.apply(null, arguments);
  70. clearTimeout(timer);
  71. }
  72. });
  73. // setup timer and call original function
  74. timer = setTimeout(timeoutCallback, milliseconds);
  75. fn.apply(null, args);
  76. });
  77. }
  78. module.exports = exports['default'];