function Promise(exec){
var self = this
self.status = 'pending'//状态没用到。。。
self.onResolvedList = []//当进行链式调用时,推入到列表
self.onRejectedList = []//当进行链式调用时,推入到列表
function resolved(reason){
// if(self.status !== 'pending')return
self.status = 'fulfilled'
setTimeout(function(){//exec为同步函数时,将任务推到后面执行
onResolvedList.forEach(function(fn){
fn(reason)
})
},0)
}
function rejected(reason){
// if(self.status !== 'pending')return
self.status = 'rejected'
setTimeout(function(){//exec为同步函数时,将任务推到后面执行
onRejectedList.forEach(function(fn){
fn(reason)
})
},0)
}
exec(resolved,rejected)//构造函数执行
}
Promise.prototype.then = function(resolved,rejected){
var self = this
self.onResolvedList.push(resolved)
self.onRejectedList.push(rejected)
return self
}
var P = new Promise(function(resolve,reject){
// setTimeout(function(){
resolve("resolve")
// }, 1000)
})
P.then(function(value){
console.log(value)
}).then(function(value){
setTimeout(function(){
console.log(value)
},1000)
})