I am having an issue migrating below two functions from generators to async / await.
When I await the endPoints
inside send()
, I get the promise return value from fixedPublisher()
as undefined.
Am I missing anything below ?
Generators:
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)];
}