Created
September 6, 2020 07:11
-
-
Save sydcanem/aca37a50793f4d6285aae072a574552e to your computer and use it in GitHub Desktop.
Promise Timer
This file contains hidden or 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
function timeout(ms, action) { | |
const timer = new Promise((resolve, reject) => { | |
const id = setTimeout(() => { | |
clearTimeout(id); | |
reject(`Timed out in ${ms}ms.`); | |
}, ms); | |
}); | |
return Promise.race([timer, action]); | |
} | |
await timeout(3000, <your_async_function>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment