【发布时间】:2020-09-04 22:16:23
【问题描述】:
我有一个异步生成器功能(批处理作业),随着时间的推移它变得相当大。 我想把它分成多个功能:
async *execute(): AsyncGenerator<ISyncState> {
await doThis();
await doThis2();
await doThis3();
yield "this";
await doThis4();
await doThis5();
await doThat();
yield "that";
// .. many more functions
}
async doThis() {...}
async doThat() {...}
async doThis2() {...}
来电者:
const gen = execute();
for await (const syncState of gen)
// do something
我想把它变成:
async *execute(): AsyncGenerator<ISyncState> {
await step1();
await step2();
await step3();
}
async step1() {
await doThis();
yield "this1"; <-- doesn't work
await doThis2();
yield "this2"; <-- doesn't work
await doThis3();
yield "this3"; <-- doesn't work
}
有没有办法从“step1()”中产生? (解决这个问题的最佳方法是什么?)
【问题讨论】:
标签: javascript typescript asynchronous ecmascript-6 generator