Skip to content

Instantly share code, notes, and snippets.

@erming
Last active March 4, 2020 19:49
Show Gist options
  • Save erming/27a2af15b156b6d7dcc64200f3e0ecae to your computer and use it in GitHub Desktop.
Save erming/27a2af15b156b6d7dcc64200f3e0ecae to your computer and use it in GitHub Desktop.
async function withTimeout(fn, ms = 400) {
return new Promise(async (resolve, reject) => {
setTimeout(() => {
reject();
}, ms);
resolve(await fn());
});
}
async function delay(ms = 0) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
withTimeout(() => delay(200)).then(_ => console.log("a")).catch(_ => console.log("b"));
withTimeout(() => delay(600)).then(_ => console.log("x")).catch(_ => console.log("y"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment