index.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. declare class TimeoutErrorClass extends Error {
  2. readonly name: 'TimeoutError';
  3. constructor(message?: string);
  4. }
  5. declare namespace pTimeout {
  6. type TimeoutError = TimeoutErrorClass;
  7. }
  8. declare const pTimeout: {
  9. /**
  10. Timeout a promise after a specified amount of time.
  11. If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out.
  12. @param input - Promise to decorate.
  13. @param milliseconds - Milliseconds before timing out.
  14. @param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`.
  15. @returns A decorated `input` that times out after `milliseconds` time.
  16. @example
  17. ```
  18. import delay = require('delay');
  19. import pTimeout = require('p-timeout');
  20. const delayedPromise = delay(200);
  21. pTimeout(delayedPromise, 50).then(() => 'foo');
  22. //=> [TimeoutError: Promise timed out after 50 milliseconds]
  23. ```
  24. */
  25. <ValueType>(
  26. input: PromiseLike<ValueType>,
  27. milliseconds: number,
  28. message?: string | Error
  29. ): Promise<ValueType>;
  30. /**
  31. Timeout a promise after a specified amount of time.
  32. If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out.
  33. @param input - Promise to decorate.
  34. @param milliseconds - Milliseconds before timing out. Passing `Infinity` will cause it to never time out.
  35. @param fallback - Do something other than rejecting with an error on timeout. You could for example retry.
  36. @returns A decorated `input` that times out after `milliseconds` time.
  37. @example
  38. ```
  39. import delay = require('delay');
  40. import pTimeout = require('p-timeout');
  41. const delayedPromise = () => delay(200);
  42. pTimeout(delayedPromise(), 50, () => {
  43. return pTimeout(delayedPromise(), 300);
  44. });
  45. ```
  46. */
  47. <ValueType, ReturnType>(
  48. input: PromiseLike<ValueType>,
  49. milliseconds: number,
  50. fallback: () => ReturnType | Promise<ReturnType>
  51. ): Promise<ValueType | ReturnType>;
  52. TimeoutError: typeof TimeoutErrorClass;
  53. // TODO: Remove this for the next major release
  54. default: typeof pTimeout;
  55. };
  56. export = pTimeout;