Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created February 3, 2020 16:09
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