//promise异步函数的请求终止 export default function markCancleble(promise){ let hasCanceled_ = false ; const wrappedPromise = new Promise((resolve,reject)=>{ // 传递过来的promise promise.then((val)=>{ hasCanceled_?reject({isCanceled:true}):resolve(val) }) promise.catch((err)=>{ hasCanceled_?reject({isCanceled:true}):resolve(err) }) return { promise:wrappedPromise, cancle(){ hasCanceled_=true } } }) } //useful 挂载到this中 this.cancleble = markCancleble (fetch()) this.cancleble.promise .then() .catch() /// //取消 this.cancleble.cancle()
原文地址:https://www.cnblogs.com/allenxieyusheng/p/8985210.html
时间: 2024-09-25 11:27:07