Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Last active Apr 23, 2020
Embed
What would you like to do?
強制的にtimeout扱いで例外を上げるwrapper function
class ForceTimeout extends Error {
get name () { return 'ForceTimeout' }
}
async function forceTimeout (time, cb) {
const intervalId = setInterval(() => {
throw new ForceTimeout()
}, time)
try {
await cb()
} catch (e) {
clearInterval(intervalId)
throw e
}
clearInterval(intervalId)
}
module.exports = forceTimeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment