Skip to content

Instantly share code, notes, and snippets.

@masnun
Created February 13, 2019 18:00
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 masnun/5a1945ce9627c9f61d896e8fd59deca5 to your computer and use it in GitHub Desktop.
Save masnun/5a1945ce9627c9f61d896e8fd59deca5 to your computer and use it in GitHub Desktop.
function wait(signal, ms) {
return new Promise((res, rej) => {
const timeOut = setTimeout(() => {
console.log("I was called");
res("ok");
}, ms);
signal.catch(err => {
rej(err);
clearTimeout(timeOut);
});
});
}
function createTimedSignal(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error(`Time out after ${ms}ms`));
}, ms);
});
}
const signal = createTimedSignal(500);
const promise = wait(signal, 1000);
promise.then(res => console.log(res)).catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment