Created
February 3, 2020 16:09
-
-
Save mattpodwysocki/2caa17c235e8e4dd4cce079cd6e896aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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