function P(fn) {
this.then = cb=> this.callback = cb
fn(value=> setTimeout(_=> this.callback(value), 0))
}
//测试
new P(resolve=> setTimeout(_=> resolve('hellow'), 1000)).then(v=> console.log(v))
function P(fn) {
this.then = cb=> this.callback = cb
fn(value=> setTimeout(_=> this.callback(value), 0))
}
//测试
new P(resolve=> setTimeout(_=> resolve('hellow'), 1000)).then(v=> console.log(v))
本文标题:2行代码实现一个极其简单的promise
本文链接:https://www.haomeiwen.com/subject/ylatmctx.html
网友评论