UNPKG

3.16 kBTypeScriptView Raw
1export 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/**
46Create 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```
53import delay from 'delay';
54
55bar();
56
57await delay(100);
58
59// Executed 100 milliseconds later
60baz();
61```
62*/
63export default function delay<T>(
64 milliseconds: number,
65 options?: Options<T>
66): Promise<T>;
67
68/**
69Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.
70
71Useful 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*/
77export function rangeDelay<T>(
78 minimum: number,
79 maximum: number,
80 options?: Options<T>
81): Promise<T>;
82
83/**
84Clears the delay and settles the promise.
85
86If you pass in a promise that is already cleared or a promise coming from somewhere else, it does nothing.
87
88@example
89```
90import delay, {clearDelay} from 'delay';
91
92const delayedPromise = delay(1000, {value: 'Done'});
93
94setTimeout(() => {
95 clearDelay(delayedPromise);
96}, 500);
97
98// 500 milliseconds later
99console.log(await delayedPromise);
100//=> 'Done'
101```
102*/
103export 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/**
107Creates 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```
111import {createDelay} from 'delay';
112
113const customDelay = createDelay({clearTimeout, setTimeout});
114
115const result = await customDelay(100, {value: '🦄'});
116
117// Executed after 100 milliseconds
118console.log(result);
119//=> '🦄'
120```
121*/
122export function createDelay(timers: {
123 clearTimeout: (timeoutId: any) => void;
124 setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown;
125}): typeof delay;