1 | export type Options<T> = {
|
2 | /**
|
3 | A value to resolve in the returned promise.
|
4 |
|
5 | @example
|
6 | ```
|
7 | import delay from 'delay';
|
8 |
|
9 | const result = await delay(100, {value: '🦄'});
|
10 |
|
11 | // Executed after 100 milliseconds
|
12 | console.log(result);
|
13 | //=> '🦄'
|
14 | ```
|
15 | */
|
16 | value?: T;
|
17 |
|
18 | /**
|
19 | An `AbortSignal` to abort the delay.
|
20 |
|
21 | The returned promise will be rejected with an `AbortError` if the signal is aborted.
|
22 |
|
23 | @example
|
24 | ```
|
25 | import delay from 'delay';
|
26 |
|
27 | const abortController = new AbortController();
|
28 |
|
29 | setTimeout(() => {
|
30 | abortController.abort();
|
31 | }, 500);
|
32 |
|
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 | ```
|
41 | */
|
42 | signal?: AbortSignal;
|
43 | };
|
44 |
|
45 | /**
|
46 | Create a promise which resolves after the specified `milliseconds`.
|
47 |
|
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: '🦄'});
|
116 |
|
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;
|