Skip to content

Instantly share code, notes, and snippets.

@sydcanem
Created September 6, 2020 07:11
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 sydcanem/aca37a50793f4d6285aae072a574552e to your computer and use it in GitHub Desktop.
Save sydcanem/aca37a50793f4d6285aae072a574552e to your computer and use it in GitHub Desktop.
Promise Timer
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