UNPKG

4.53 kBTypeScriptView Raw
1/**
2 * The `timer` module exposes a global API for scheduling functions to
3 * be called at some future period of time. Because the timer functions are
4 * globals, there is no need to call `require('timers')` to use the API.
5 *
6 * The timer functions within Node.js implement a similar API as the timers API
7 * provided by Web Browsers but use a different internal implementation that is
8 * built around the Node.js [Event Loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout).
9 * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/timers.js)
10 */
11declare module 'timers' {
12 import { Abortable } from 'node:events';
13 import { setTimeout as setTimeoutPromise, setImmediate as setImmediatePromise, setInterval as setIntervalPromise } from 'node:timers/promises';
14 interface TimerOptions extends Abortable {
15 /**
16 * Set to `false` to indicate that the scheduled `Timeout`
17 * should not require the Node.js event loop to remain active.
18 * @default true
19 */
20 ref?: boolean | undefined;
21 }
22 let setTimeout: typeof global.setTimeout;
23 let clearTimeout: typeof global.clearTimeout;
24 let setInterval: typeof global.setInterval;
25 let clearInterval: typeof global.clearInterval;
26 let setImmediate: typeof global.setImmediate;
27 let clearImmediate: typeof global.clearImmediate;
28 global {
29 namespace NodeJS {
30 // compatibility with older typings
31 interface Timer extends RefCounted {
32 hasRef(): boolean;
33 refresh(): this;
34 [Symbol.toPrimitive](): number;
35 }
36 interface Immediate extends RefCounted {
37 /**
38 * If true, the `Immediate` object will keep the Node.js event loop active.
39 * @since v11.0.0
40 */
41 hasRef(): boolean;
42 _onImmediate: Function; // to distinguish it from the Timeout class
43 }
44 interface Timeout extends Timer {
45 /**
46 * If true, the `Timeout` object will keep the Node.js event loop active.
47 * @since v11.0.0
48 */
49 hasRef(): boolean;
50 /**
51 * Sets the timer's start time to the current time, and reschedules the timer to
52 * call its callback at the previously specified duration adjusted to the current
53 * time. This is useful for refreshing a timer without allocating a new
54 * JavaScript object.
55 *
56 * Using this on a timer that has already called its callback will reactivate the
57 * timer.
58 * @since v10.2.0
59 * @return a reference to `timeout`
60 */
61 refresh(): this;
62 [Symbol.toPrimitive](): number;
63 }
64 }
65 function setTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timeout;
66 // util.promisify no rest args compability
67 // tslint:disable-next-line void-return
68 function setTimeout(callback: (args: void) => void, ms?: number): NodeJS.Timeout;
69 namespace setTimeout {
70 const __promisify__: typeof setTimeoutPromise;
71 }
72 function clearTimeout(timeoutId: NodeJS.Timeout): void;
73 function setInterval<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs): NodeJS.Timer;
74 // util.promisify no rest args compability
75 // tslint:disable-next-line void-return
76 function setInterval(callback: (args: void) => void, ms?: number): NodeJS.Timer;
77 namespace setInterval {
78 const __promisify__: typeof setIntervalPromise;
79 }
80 function clearInterval(intervalId: NodeJS.Timeout): void;
81 function setImmediate<TArgs extends any[]>(callback: (...args: TArgs) => void, ...args: TArgs): NodeJS.Immediate;
82 // util.promisify no rest args compability
83 // tslint:disable-next-line void-return
84 function setImmediate(callback: (args: void) => void): NodeJS.Immediate;
85 namespace setImmediate {
86 const __promisify__: typeof setImmediatePromise;
87 }
88 function clearImmediate(immediateId: NodeJS.Immediate): void;
89 function queueMicrotask(callback: () => void): void;
90 }
91}
92declare module 'node:timers' {
93 export * from 'timers';
94}