Last active
April 23, 2020 14:09
-
-
Save wtnabe/759cb1a6a9e1217802a2e8eccd9deb15 to your computer and use it in GitHub Desktop.
強制的にtimeout扱いで例外を上げるwrapper function
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
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