timeout.js 2.6 KB

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