Created
February 13, 2019 17:28
-
-
Save masnun/2a8a3646eb85f1468733a43113f2fc93 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(ms) { | |
const ret = {}; | |
const signal = new Promise((resolve, reject) => { | |
ret.cancel = () => { | |
reject(new Error("Promise was cancelled")); | |
}; | |
}); | |
ret.promise = new Promise((res, rej) => { | |
const timeOut = setTimeout(() => { | |
console.log("I was called"); | |
res("ok"); | |
}, ms); | |
signal.catch(err => { | |
rej(err); | |
clearTimeout(timeOut); | |
}); | |
}); | |
return ret; | |
} | |
const { promise, cancel } = wait(1000); | |
promise.then(res => console.log(res)).catch(err => console.log(err)); | |
cancel(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment