Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created March 12, 2020 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/88aae4efaebf86454ab51f038f2581f9 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/88aae4efaebf86454ab51f038f2581f9 to your computer and use it in GitHub Desktop.
import { AbortError } from '../aborterror';
export function sleep(dueTime: number, signal?: AbortSignal) {
return new Promise<void>((resolve, reject) => {
if (signal?.aborted) {
reject(new AbortError());
}
const id = setTimeout(() => {
if (signal?.aborted) {
reject(new AbortError());
}
resolve();
}, dueTime);
if (signal) {
signal.onabort = () => {
clearTimeout(id);
reject(new AbortError());
};
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment