tj/co 源码 --》更了解async await

// tj  async+await = generator +co
function co(it){
    return new Promise((resolve,reject)=>{
        // 异步迭代 需要next函数
        function next(r){
            let {value,done} = it.next(r);
            if(done){
                resolve(value);
            }else{
                Promise.resolve(value).then(data=>{
                    next(data);
                },reject)
            }
        }
        next();
    })
}
co(readAge()).then(data=>{
    console.log(data);
});

 

posted @ 2020-06-09 22:15  TTtttt5  阅读(205)  评论(0编辑  收藏  举报