summaryrefslogtreecommitdiffstats
path: root/debian/tests/test_modules/delay/index.d.ts
blob: d3d404b3241df21ca6d336de7df67e767dbcda4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
declare namespace delay {
	interface ClearablePromise<T> extends Promise<T> {
		/**
		Clears the delay and settles the promise.
		*/
		clear(): void;
	}

	/**
	Minimal subset of `AbortSignal` that delay will use if passed.
	This avoids a dependency on dom.d.ts.
	The dom.d.ts `AbortSignal` is compatible with this one.
	*/
	interface AbortSignal {
		readonly aborted: boolean;
		addEventListener(
			type: 'abort',
			listener: () => void,
			options?: {once?: boolean}
		): void;
		removeEventListener(type: 'abort', listener: () => void): void;
	}

	interface Options {
		/**
		An optional AbortSignal to abort the delay.
		If aborted, the Promise will be rejected with an AbortError.
		*/
		signal?: AbortSignal;
	}
}

type Delay = {
	/**
	Create a promise which resolves after the specified `milliseconds`.

	@param milliseconds - Milliseconds to delay the promise.
	@returns A promise which resolves after the specified `milliseconds`.
	*/
	(milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>;

	/**
	Create a promise which resolves after the specified `milliseconds`.

	@param milliseconds - Milliseconds to delay the promise.
	@returns A promise which resolves after the specified `milliseconds`.
	*/
	<T>(
		milliseconds: number,
		options?: delay.Options & {
			/**
			Value to resolve in the returned promise.
			*/
			value: T;
		}
	): delay.ClearablePromise<T>;

	/**
	Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.

	Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead.

	@param minimum - Minimum amount of milliseconds to delay the promise.
	@param maximum - Maximum amount of milliseconds to delay the promise.
	@returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed.
	*/
	range<T>(
		minimum: number,
		maximum: number,
		options?: delay.Options & {
			/**
			Value to resolve in the returned promise.
			*/
			value: T;
		}
	): delay.ClearablePromise<T>;

	// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 is resolved.
	/**
	Create a promise which rejects after the specified `milliseconds`.

	@param milliseconds - Milliseconds to delay the promise.
	@returns A promise which rejects after the specified `milliseconds`.
	*/
	reject(
		milliseconds: number,
		options?: delay.Options & {
			/**
			Value to reject in the returned promise.
			*/
			value?: unknown;
		}
	): delay.ClearablePromise<never>;
};

declare const delay: Delay & {
	// The types are intentionally loose to make it work with both Node.js and browser versions of these methods.
	createWithTimers(timers: {
		clearTimeout: (timeoutId: any) => void;
		setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown;
	}): Delay;

	// TODO: Remove this for the next major release.
	default: typeof delay;
};

export = delay;