//promise英文中叫做承诺
//成功resolve 失败reject 正在过程中 pending 没人接的时候
let obj = {
status: '',
value: ''
}
function Apromise(fn) {
obj.status = 'pending';
obj.value = '';
function resolve(value) {
if (obj.status = 'pending') {
obj.status = 'resolve';
obj.value = value;
}
}
function reject(value) {
if (obj.status = 'pending') {
obj.status = 'reject';
obj.value = value;
}
}
fn(resolve, reject)
}
Apromise.prototype.then = function(resolve, reject) {
if (obj.status === 'resolve') {
resolve(obj.value)
}
if (obj.status === 'reject') {
reject(obj.value)
}
}
let ap = new Apromise((resolve, reject) => {
// resolve('我是成功的');
reject('我是失败的');
}).then(res => {
console.log(res);
},err=>{
console.log(err);
})
实例化promise:相当于如果有成功的条件就把function Apromise中的成功函数放到最下面进行执行
.then:相当于把then中的函数放入Apromise.prototype.then中进行根据之前设置的状态执行输出(obj.value)
总结 没看源码现在感觉就是形参实参的来回调用 没啥的