Pormise

//Pormise
function MyPromise (executor) {
var self = this
self.status = 'pending'
self.resolveValue = null
self.rejectValue = null
self.resolveCallbackList = [] //用来存储resolve的回调函数
self.rejectCallbackList = [] //用来存储reject的回调函数

function resolve (value) {
if (self.status === 'pending') {
self.status = 'Fulfilled'
self.resolveValue = value
self.resolveCallbackList.forEach(function (item) {
item()
})
}
}

function reject (value) {
if(self.status === 'pending') {
self.status = 'Rejected'
self.rejectValue = value
self.rejectCallbackList.forEach(function (item) {
item()
})
}
}
try {
executor(resolve, reject)
} catch (e) {
reject(e)
}
}
function resultIsPromise (nextValue, res, rej) {
if(nextValue instanceof MyPromise) {
nextValue.then(function (val) {
res(val)
}, function(reason) {
rej(reason)
})
} else {
res(nextValue)
}
}
MyPromise.prototype.then = function (resolve, reject) {
if (!resolve) {
resolve = function(val) {
return val
}
}
if (!reject) {
reject = function (val) {
throw new Error(val)
}
}
var self = this
var nextPromise = new MyPromise(function(res, rej) {
setTimeout(function() {
if (self.status === 'Fulfilled') {
try{
// var newResolveVal = resolve(self.resolveValue)
// res(newResolveVal)
var newResolveVal = resolve(self.resolveValue)
resultIsPromise(newResolveVal, res, rej)
} catch(e) {
rej(e)
}
}
}, 0)

setTimeout(function () {
if (self.status === 'Rejected') {
try{
var newRejectVal = reject(self.rejectValue)
resultIsPromise(newRejectVal, res, rej)
} catch(e) {
rej(e)
}
}
}, 0)

if (self.status === 'pending') {
if(typeof resolve === 'function') {
self.resolveCallbackList.push(function () {
setTimeout(function() {
try{
var newResolveVal = resolve(self.resolveValue)
resultIsPromise(newResolveVal, res, rej)
} catch (e) {
rej(e)
}
})
})
}
if(typeof reject === 'function') {
setTimeout(function () {
self.rejectCallbackList.push(function () {
try{
var newRejectVal = reject(self.rejectValue)
resultIsPromise(newRejectVal, res, rej)
} catch(e) {
rej(e)
}
})
}, 0)
}
}
})
return nextPromise
}
//race
MyPromise.race = function (promises) {
return new MyPromise(function (res, rej) {
promises.forEach( function (item) {
item.then(res, rej)
})
})
}
MyPromise.resolve = function (val) {
return new MyPromise(function (resolve) {
resolve(val)
})
}
MyPromise.catch = function (val) {
return new MyPromise(function (undefined,reject) {
reject(val)
})
}
MyPromise.all = function (promises) {
return new MyPromise(function(resolve, reject){
var countNum=0;
var promiseNum=promises.length;
var resolvedValue=[];
for(var i=0;i<promiseNum;i++){
(function(i){
promises[i].then(function(value){
countNum++;
resolvedValue[i]=value;
if(countNum===promiseNum){
return resolve(resolvedValue)
}
},function(reason){
return reject(reason)
})
})(i)
}
})
}
 
 

 

转载于:https://www.cnblogs.com/CoderZX/p/10792216.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值