我遇到了从生成器迁移到async / await两个函数以下的问题。 当我等待send()中的endPoints时,我将来自fixedPublisher()的promise返回值视为未定义。 我错过了下面的任何内容吗?
发电机
export function* fixedPublisher(endpoints: any): any {
while (endpoints !== undefined) {
yield co(function* send(): any {
return endpoints;
});
}
}
function* send(publisher: any, rnd: any): any {
const p = publisher.next();
if (p.done) {
throw new Error('publisher is done');
}
const endpoints = yield p.value;
if (!endpoints || endpoints.length === 0) {
throw new Error('publisher did not return endpoints');
}
const m = Math.max(endpoints.length - 1, 0);
return endpoints[rnd.integer(0, m)];
}
Async / Await
export async function fixedPublisher(endpoints: any): Promise<any> {
return await co(endpoints);
}
function* send(publisher: any, rnd: any): any {
const endpoints = await (publisher);
if (!endpoints || endpoints.length === 0) {
throw new Error('publisher did not return endpoints');
}
const m = Math.max(endpoints.length - 1, 0);
return endpoints[rnd.integer(0, m)];
}