Created
February 13, 2019 18:00
-
-
Save masnun/5a1945ce9627c9f61d896e8fd59deca5 to your computer and use it in GitHub Desktop.
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 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