JavaScript 异步编程返回构造实例对象时,为什么要传一个参数,并且fn2为什么打印的是如下代码:
res => {
this.value = res
}
如图所示:
代码附上:
function Aiesec (fn) {
this.value = undefined;
this.then = (resolve) => {
return new Aiesec(fn2 => {
// console.log(fn2)
this.value = resolve(this.value)
fn2(this.value)
})
},
fn(res => {
this.value = res
})
}
var aiesec = new Aiesec((resolve) => {
resolve(1)
}).then(res => {
console.log(res)
return res + 1;
}).then(res => {
console.log(res)
})
```~~~~
异步就是靠回调呀。这个看上去自己实现了个Promise