Skip to content

Commit

Permalink
Add unref support to delay()
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Feb 12, 2024
1 parent 09680d8 commit f8d12b6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/promises.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
export type MaybePromise<T> = T | Promise<T>;

export const delay = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms)
);
export const delay = (ms: number, options: {
/**
* If set, if the code is running in a compatible runtime (i.e. Node.js)
* then the .unref() will be used to ensure that this delay does not
* block the process shutdown.
*/
unref?: boolean
} = {}) =>
new Promise((resolve) => {
const timer = setTimeout(resolve, ms);

if (options.unref && (timer as any).unref) {
(timer as any).unref();
}
});

export async function doWhile<T>(
doFn: () => Promise<T>,
Expand Down

0 comments on commit f8d12b6

Please sign in to comment.