Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Last active April 23, 2020 14: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 wtnabe/759cb1a6a9e1217802a2e8eccd9deb15 to your computer and use it in GitHub Desktop.
Save wtnabe/759cb1a6a9e1217802a2e8eccd9deb15 to your computer and use it in GitHub Desktop.
強制的に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