码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-03 15:30:30      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:reject   can   his   false   har   cat   函数   efault   cti   

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

 

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

标签:reject   can   his   false   har   cat   函数   efault   cti   

原文地址:https://www.cnblogs.com/allenxieyusheng/p/8985210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!