generator 就是 coroutine. 以下是一段使用 coroutine 实现 generator 的代码:
async generator(result) {
for (int i = 0; i < 10; ++i) {
result.set(i);
}
result.set(null);
}
async caller() {
let result = new Event();
generator(result);
while (true) {
let i = await result.wait();
result.clear();
if (i === null) {
break;
}
console.debug(result);
}
}
【 在 pangwa (学门手艺,混口饭吃.) 的大作中提到: 】
: 现有的promise也有些支持cancel啊,不过大多都是半残(因为要做好必须成体系的支持),至于promise和async await没必要区分,本来就能混用
: 对了 你说用generator实现协程的问题能简单说下吗?
: 发自「今日水木 on iPhone X」
: ...................
--
修改:hgoldfish FROM 110.85.22.*
FROM 110.85.22.*