common: async queue wrapper for generator

This commit is contained in:
Koushik Dutta
2024-07-11 21:26:13 -07:00
parent 3dcb36adf9
commit 46616467f4

View File

@@ -155,6 +155,23 @@ export function createAsyncQueue<T>() {
}
}
export function createAsyncQueueFromGenerator<T>(generator: AsyncGenerator<T>) {
const q = createAsyncQueue<T>();
(async() => {
try {
for await (const i of generator) {
q.submit(i);
}
}
catch (e) {
q.end(e as Error);
}
q.end();
})();
return q;
}
// async function testSlowEnqueue() {
// const asyncQueue = createAsyncQueue<number>();