We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 334d0fe commit fa80984Copy full SHA for fa80984
1 file changed
src/index.ts
@@ -10,8 +10,19 @@ const setTimeoutBypassingFakes = global.setTimeout;
10
*
11
* @param ts - how long to sleep for.
12
*/
13
-export const sleep = (ms: number): Promise<void> =>
14
- new Promise((resolve) => setTimeoutBypassingFakes(resolve, ms));
+export const sleep = (
+ ms: number,
15
+ unref = false,
16
+): Promise<void> & { timeout: NodeJS.Timeout } => {
17
+ let timeout!: NodeJS.Timeout;
18
+ const promise = new Promise<void>((resolve) => {
19
+ timeout = setTimeoutBypassingFakes(resolve, ms);
20
+ });
21
+ if (unref) {
22
+ timeout.unref();
23
+ }
24
+ return Object.assign(promise, { timeout });
25
+};
26
27
/**
28
* Polls until the condition passes.
0 commit comments