9595e992642fe9a2a9c50338b04169f6019f74acbf806b9c73d860a2509313d6a0f7baae91e25b7968c22384c7c407b19bb7fa9252bbb6fb75ba6219f6a006 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * The `timers/promises` API provides an alternative set of timer functions
  3. * that return `Promise` objects. The API is accessible via
  4. * `require('node:timers/promises')`.
  5. *
  6. * ```js
  7. * import {
  8. * setTimeout,
  9. * setImmediate,
  10. * setInterval,
  11. * } from 'node:timers/promises';
  12. * ```
  13. * @since v15.0.0
  14. * @see [source](https://github.com/nodejs/node/blob/v22.x/lib/timers/promises.js)
  15. */
  16. declare module "timers/promises" {
  17. import { TimerOptions } from "node:timers";
  18. /**
  19. * ```js
  20. * import {
  21. * setTimeout,
  22. * } from 'node:timers/promises';
  23. *
  24. * const res = await setTimeout(100, 'result');
  25. *
  26. * console.log(res); // Prints 'result'
  27. * ```
  28. * @since v15.0.0
  29. * @param delay The number of milliseconds to wait before fulfilling the
  30. * promise. **Default:** `1`.
  31. * @param value A value with which the promise is fulfilled.
  32. */
  33. function setTimeout<T = void>(delay?: number, value?: T, options?: TimerOptions): Promise<T>;
  34. /**
  35. * ```js
  36. * import {
  37. * setImmediate,
  38. * } from 'node:timers/promises';
  39. *
  40. * const res = await setImmediate('result');
  41. *
  42. * console.log(res); // Prints 'result'
  43. * ```
  44. * @since v15.0.0
  45. * @param value A value with which the promise is fulfilled.
  46. */
  47. function setImmediate<T = void>(value?: T, options?: TimerOptions): Promise<T>;
  48. /**
  49. * Returns an async iterator that generates values in an interval of `delay` ms.
  50. * If `ref` is `true`, you need to call `next()` of async iterator explicitly
  51. * or implicitly to keep the event loop alive.
  52. *
  53. * ```js
  54. * import {
  55. * setInterval,
  56. * } from 'node:timers/promises';
  57. *
  58. * const interval = 100;
  59. * for await (const startTime of setInterval(interval, Date.now())) {
  60. * const now = Date.now();
  61. * console.log(now);
  62. * if ((now - startTime) > 1000)
  63. * break;
  64. * }
  65. * console.log(Date.now());
  66. * ```
  67. * @since v15.9.0
  68. * @param delay The number of milliseconds to wait between iterations.
  69. * **Default:** `1`.
  70. * @param value A value with which the iterator returns.
  71. */
  72. function setInterval<T = void>(delay?: number, value?: T, options?: TimerOptions): NodeJS.AsyncIterator<T>;
  73. interface Scheduler {
  74. /**
  75. * An experimental API defined by the [Scheduling APIs](https://github.com/WICG/scheduling-apis) draft specification
  76. * being developed as a standard Web Platform API.
  77. *
  78. * Calling `timersPromises.scheduler.wait(delay, options)` is roughly equivalent
  79. * to calling `timersPromises.setTimeout(delay, undefined, options)` except that
  80. * the `ref` option is not supported.
  81. *
  82. * ```js
  83. * import { scheduler } from 'node:timers/promises';
  84. *
  85. * await scheduler.wait(1000); // Wait one second before continuing
  86. * ```
  87. * @since v17.3.0, v16.14.0
  88. * @experimental
  89. * @param delay The number of milliseconds to wait before resolving the
  90. * promise.
  91. */
  92. wait(delay: number, options?: { signal?: AbortSignal }): Promise<void>;
  93. /**
  94. * An experimental API defined by the [Scheduling APIs](https://github.com/WICG/scheduling-apis) draft specification
  95. * being developed as a standard Web Platform API.
  96. *
  97. * Calling `timersPromises.scheduler.yield()` is equivalent to calling
  98. * `timersPromises.setImmediate()` with no arguments.
  99. * @since v17.3.0, v16.14.0
  100. * @experimental
  101. */
  102. yield(): Promise<void>;
  103. }
  104. const scheduler: Scheduler;
  105. }
  106. declare module "node:timers/promises" {
  107. export * from "timers/promises";
  108. }