建议写的时候参照new Promise的过程去写
function mypromise(fn){
const that = this;
that.status = 'pedding';
that.value = '';
let falg =false
//当没有调用resolve,reject函数时抛出错误
function resolve(val){
if( that.status == 'pedding'){
that.status = 'resolve';
that.value = val;
falg = true
}
}
function reject(val){
if( that.status == 'pedding'){
that.status = 'reject';
that.value = val;
falg = true
}
}
fn(resolve,reject)
if(!falg) throw new Error("错误!!");
}
// new Promise((resolve,reject) =>{
// resolve()
// })
mypromise.prototype.then = function(onresolve,onreject){
if(this.status==='resolve'){
onresolve(this.value)
//将值传给函数
}
if(this.status==='reject'){
onreject(this.value)
//将值传给函数
}
}
new mypromise((resolve,reject) => {
reject(11)
}).then(res => {
console.log(res)
},err => {
console.log(err);
})