async函数的实现: generator iterator co
async function read(){
let template = await readFile(‘./teamplate.txt’)
let data = await readFile(‘./data.txt’)
return template + data
}
等价于
function read() {
return co( function *(){
let template = yield readFile(‘./template.txt’)
let data = yield readFile(‘./data.txt’)
return template + data
})
}
co包裹函数:
function co (gen) {
const it = gen()
return new Promise((resolve, reject){
(function next(lastVal){
const {value,done} = it.next()
if(done){
resolve(value)
}
return Promise.resolve(value).then(next ,reason=>reject(reason))
})()
}
}
标签:function,return,函数,co,实现,template,async,txt,data From: https://www.cnblogs.com/eret9616/p/17145237.html