Skip to content

Instantly share code, notes, and snippets.

@ZWkang
Created June 8, 2021 13:43
Show Gist options
  • Select an option

  • Save ZWkang/c639a053ea38fa7c1a30ad94a3a81b86 to your computer and use it in GitHub Desktop.

Select an option

Save ZWkang/c639a053ea38fa7c1a30ad94a3a81b86 to your computer and use it in GitHub Desktop.
提前终止promise链式调用 stop promise chain run
let cancel = null;
const canceler = new Cancel((c) => cancel = c);
function Cancel( exc ) {
let token = this;
let pendingPromise = null;
this.promise = new Promise((resolve)=> {
pendingPromise = resolve;
})
exc(function e(message){
pendingPromise(message)
})
}
function Progress(config) {
return new Promise((resolve, reject) => {
if(config.cancelToken){
config.cancelToken.promise.then((_resolve) => {
reject(123)
})
}
config.callback(resolve, reject);
})
}
Progress({
callback: (resolve, reject) => {
setTimeout(() => { resolve(4000) }, 4000)
},
cancelToken: canceler,
}).then((val) => { console.log(val) }).catch(reason => console.log('fail',reason))
setTimeout(() => {cancel()}, 2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment