Promise 异步函数的加上外壳终止Promise

//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