Last active
April 7, 2021 10:41
-
-
Save QuarkGluonPlasma/d0d774bd544006c1194c8547bdafc6ba to your computer and use it in GitHub Desktop.
可取消的promise
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 cancable(p) { | |
var cancel; | |
var promise = new Promise((resolve, reject) => { | |
p.then(resolve, reject); | |
cancel = reject; | |
}); | |
return { | |
promise: promise, | |
cancel: cancel | |
}; | |
}; | |
const { promise, cancel } = cancable(p); | |
promise.then(() => { | |
console.log('sucess'); | |
}).catch((msg) => { | |
console.log(msg); | |
}) | |
cancel('cancel'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment