function co(it: Generator): Promise<any> {
return new Promise((resolve, reject) => {
next();
function next(val?: any) {
const { value, done } = it.next(val);
if (done) return resolve(value);
Promise.resolve(value).then(data => {
next(data);
}, reject);
}
});
}
function * read() {
const r: string = yield fs.readFile(_path('name.txt'), 'utf8');
const age: string = yield fs.readFile(_path(r), 'utf8');
const result: string[] = yield [1 + age];
return result
}
co(read()).then((data: string) => {
console.log(data);
}).catch((err: any) => {
console.log('error: ', err);
});
co原理实现
最新推荐文章于 2021-10-21 14:43:43 发布
3897

被折叠的 条评论
为什么被折叠?



