Skip to content

Commit fa80984

Browse files
committed
Ability for sleep to unref
1 parent 334d0fe commit fa80984

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ const setTimeoutBypassingFakes = global.setTimeout;
1010
*
1111
* @param ts - how long to sleep for.
1212
*/
13-
export const sleep = (ms: number): Promise<void> =>
14-
new Promise((resolve) => setTimeoutBypassingFakes(resolve, ms));
13+
export const sleep = (
14+
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+
};
1526

1627
/**
1728
* Polls until the condition passes.

0 commit comments

Comments
 (0)