看网上手写promise,看到这个部分就看不懂了
class MyPromise{
constructor(func){
this.state = 'PENDING';
console.log(this.state,'11')
func(this.resolve.bind(this))
}
resolve(){
this.state = 'REJECTED'
}
then(){
return new MyPromise(res=>{
console.log(this.state,'22')
})
}
}
new MyPromise(res=>{
res()
}).then()
打印:
为什么state赋值了PENDING,回调里state=REJECTED
看图