Skip to content

Instantly share code, notes, and snippets.

@zhanglun
Created July 19, 2020 01:47
Show Gist options
  • Save zhanglun/11ddf7c3ea522dd3b7d388d060e1f48c to your computer and use it in GitHub Desktop.
Save zhanglun/11ddf7c3ea522dd3b7d388d060e1f48c to your computer and use it in GitHub Desktop.
wrapPromiseWithAbort.js
let wrapPromiseWithAbort = function (promise) {
let _abort = null
let pAbort = new Promise ((resolve, reject) => {
_abort = function (reason = 'abort!') {
console.warn(reason)
rej(reason)
}
})
let race = Promise.rece([promise, pAbort])
race.abort = _abort
console.log(priomise, pAbort)
return race
}
let p1 = new Promise((res) => {
setTimout(() => {
res('p1 success')
}, 2000)
})
let testP = PromiseWithAbort(p1)
testP.then((res) => {
console.log('success: ', res)
}, (error) => {
console.log('error: ', error)
})
testP.abort()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment