Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created February 3, 2020 16:09
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/2caa17c235e8e4dd4cce079cd6e896aa to your computer and use it in GitHub Desktop.
Save mattpodwysocki/2caa17c235e8e4dd4cce079cd6e896aa to your computer and use it in GitHub Desktop.
import { AbortError } from "../util/aborterror";
export function delay(action: () => void, dueTime: number, signal?: AbortSignal) {
return new Promise((resolve, reject) => {
if (signal?.aborted) {
throw new AbortError();
}
const id = setTimeout(() => {
if (signal?.aborted) {
throw new AbortError();
}
try {
action();
resolve();
} catch (e) {
reject(e);
}
}, 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