|
1 | | -declare namespace delay { |
2 | | - interface ClearablePromise<T> extends Promise<T> { |
3 | | - /** |
4 | | - Clears the delay and settles the promise. |
5 | | - */ |
6 | | - clear(): void; |
7 | | - } |
8 | | - |
| 1 | +export type Options<T> = { |
9 | 2 | /** |
10 | | - Minimal subset of `AbortSignal` that delay will use if passed. |
11 | | - This avoids a dependency on dom.d.ts. |
12 | | - The dom.d.ts `AbortSignal` is compatible with this one. |
13 | | - */ |
14 | | - interface AbortSignal { |
15 | | - readonly aborted: boolean; |
16 | | - addEventListener( |
17 | | - type: 'abort', |
18 | | - listener: () => void, |
19 | | - options?: {once?: boolean} |
20 | | - ): void; |
21 | | - removeEventListener(type: 'abort', listener: () => void): void; |
22 | | - } |
| 3 | + A value to resolve in the returned promise. |
23 | 4 |
|
24 | | - interface Options { |
25 | | - /** |
26 | | - An optional AbortSignal to abort the delay. |
27 | | - If aborted, the Promise will be rejected with an AbortError. |
28 | | - */ |
29 | | - signal?: AbortSignal; |
30 | | - } |
31 | | -} |
| 5 | + @example |
| 6 | + ``` |
| 7 | + import delay from 'delay'; |
32 | 8 |
|
33 | | -type Delay = { |
34 | | - /** |
35 | | - Create a promise which resolves after the specified `milliseconds`. |
| 9 | + const result = await delay(100, {value: '🦄'}); |
36 | 10 |
|
37 | | - @param milliseconds - Milliseconds to delay the promise. |
38 | | - @returns A promise which resolves after the specified `milliseconds`. |
| 11 | + // Executed after 100 milliseconds |
| 12 | + console.log(result); |
| 13 | + //=> '🦄' |
| 14 | + ``` |
39 | 15 | */ |
40 | | - (milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>; |
| 16 | + value?: T; |
41 | 17 |
|
42 | 18 | /** |
43 | | - Create a promise which resolves after the specified `milliseconds`. |
| 19 | + An `AbortSignal` to abort the delay. |
44 | 20 |
|
45 | | - @param milliseconds - Milliseconds to delay the promise. |
46 | | - @returns A promise which resolves after the specified `milliseconds`. |
47 | | - */ |
48 | | - <T>( |
49 | | - milliseconds: number, |
50 | | - options?: delay.Options & { |
51 | | - /** |
52 | | - Value to resolve in the returned promise. |
53 | | - */ |
54 | | - value: T; |
55 | | - } |
56 | | - ): delay.ClearablePromise<T>; |
| 21 | + The returned promise will be rejected with an `AbortError` if the signal is aborted. |
57 | 22 |
|
58 | | - /** |
59 | | - Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. |
| 23 | + @example |
| 24 | + ``` |
| 25 | + import delay from 'delay'; |
60 | 26 |
|
61 | | - 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. |
| 27 | + const abortController = new AbortController(); |
62 | 28 |
|
63 | | - @param minimum - Minimum amount of milliseconds to delay the promise. |
64 | | - @param maximum - Maximum amount of milliseconds to delay the promise. |
65 | | - @returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed. |
66 | | - */ |
67 | | - range<T>( |
68 | | - minimum: number, |
69 | | - maximum: number, |
70 | | - options?: delay.Options & { |
71 | | - /** |
72 | | - Value to resolve in the returned promise. |
73 | | - */ |
74 | | - value: T; |
75 | | - } |
76 | | - ): delay.ClearablePromise<T>; |
77 | | - |
78 | | - // TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 is resolved. |
79 | | - /** |
80 | | - Create a promise which rejects after the specified `milliseconds`. |
| 29 | + setTimeout(() => { |
| 30 | + abortController.abort(); |
| 31 | + }, 500); |
81 | 32 |
|
82 | | - @param milliseconds - Milliseconds to delay the promise. |
83 | | - @returns A promise which rejects after the specified `milliseconds`. |
| 33 | + try { |
| 34 | + await delay(1000, {signal: abortController.signal}); |
| 35 | + } catch (error) { |
| 36 | + // 500 milliseconds later |
| 37 | + console.log(error.name) |
| 38 | + //=> 'AbortError' |
| 39 | + } |
| 40 | + ``` |
84 | 41 | */ |
85 | | - reject( |
86 | | - milliseconds: number, |
87 | | - options?: delay.Options & { |
88 | | - /** |
89 | | - Value to reject in the returned promise. |
90 | | - */ |
91 | | - value?: unknown; |
92 | | - } |
93 | | - ): delay.ClearablePromise<never>; |
| 42 | + signal?: AbortSignal; |
94 | 43 | }; |
95 | 44 |
|
96 | | -declare const delay: Delay & { |
97 | | - // The types are intentionally loose to make it work with both Node.js and browser versions of these methods. |
98 | | - createWithTimers(timers: { |
99 | | - clearTimeout: (timeoutId: any) => void; |
100 | | - setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown; |
101 | | - }): Delay; |
| 45 | +/** |
| 46 | +Create a promise which resolves after the specified `milliseconds`. |
102 | 47 |
|
103 | | - // TODO: Remove this for the next major release. |
104 | | - default: typeof delay; |
105 | | -}; |
| 48 | +@param milliseconds - Milliseconds to delay the promise. |
| 49 | +@returns A promise which resolves after the specified `milliseconds`. |
| 50 | +
|
| 51 | +@example |
| 52 | +``` |
| 53 | +import delay from 'delay'; |
| 54 | +
|
| 55 | +bar(); |
| 56 | +
|
| 57 | +await delay(100); |
| 58 | +
|
| 59 | +// Executed 100 milliseconds later |
| 60 | +baz(); |
| 61 | +``` |
| 62 | +*/ |
| 63 | +export default function delay<T>( |
| 64 | + milliseconds: number, |
| 65 | + options?: Options<T> |
| 66 | +): Promise<T>; |
| 67 | + |
| 68 | +/** |
| 69 | +Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. |
| 70 | +
|
| 71 | +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 this method, you could give it a threshold instead. |
| 72 | +
|
| 73 | +@param minimum - Minimum amount of milliseconds to delay the promise. |
| 74 | +@param maximum - Maximum amount of milliseconds to delay the promise. |
| 75 | +@returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed. |
| 76 | +*/ |
| 77 | +export function rangeDelay<T>( |
| 78 | + minimum: number, |
| 79 | + maximum: number, |
| 80 | + options?: Options<T> |
| 81 | +): Promise<T>; |
| 82 | + |
| 83 | +/** |
| 84 | +Clears the delay and settles the promise. |
| 85 | +
|
| 86 | +If you pass in a promise that is already cleared or a promise coming from somewhere else, it does nothing. |
| 87 | +
|
| 88 | +@example |
| 89 | +``` |
| 90 | +import delay, {clearDelay} from 'delay'; |
| 91 | +
|
| 92 | +const delayedPromise = delay(1000, {value: 'Done'}); |
| 93 | +
|
| 94 | +setTimeout(() => { |
| 95 | + clearDelay(delayedPromise); |
| 96 | +}, 500); |
| 97 | +
|
| 98 | +// 500 milliseconds later |
| 99 | +console.log(await delayedPromise); |
| 100 | +//=> 'Done' |
| 101 | +``` |
| 102 | +*/ |
| 103 | +export function clearDelay(delayPromise: Promise<unknown>): void; |
| 104 | + |
| 105 | +// The types are intentionally loose to make it work with both Node.js and browser versions of these methods. |
| 106 | +/** |
| 107 | +Creates a new `delay` instance using the provided functions for clearing and setting timeouts. Useful if you're about to stub timers globally, but you still want to use `delay` to manage your tests. |
| 108 | +
|
| 109 | +@example |
| 110 | +``` |
| 111 | +import {createDelay} from 'delay'; |
| 112 | +
|
| 113 | +const customDelay = createDelay({clearTimeout, setTimeout}); |
| 114 | +
|
| 115 | +const result = await customDelay(100, {value: '🦄'}); |
106 | 116 |
|
107 | | -export = delay; |
| 117 | +// Executed after 100 milliseconds |
| 118 | +console.log(result); |
| 119 | +//=> '🦄' |
| 120 | +``` |
| 121 | +*/ |
| 122 | +export function createDelay(timers: { |
| 123 | + clearTimeout: (timeoutId: any) => void; |
| 124 | + setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown; |
| 125 | +}): typeof delay; |
0 commit comments