Created
August 1, 2022 00:24
-
-
Save justsml/d48c16e2301789db2aad08d7af39e119 to your computer and use it in GitHub Desktop.
Examples of using for-await-of syntax to consume streams.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const batchStream = (size: number) => | |
async function* batchStream<TStreamType>(stream: AsyncIterable<TStreamType>) { | |
let buffer: TStreamType[] = []; | |
for await (const chunk of stream) { | |
buffer.push(chunk); | |
if (buffer.length >= size) { | |
yield buffer; | |
buffer = []; | |
} | |
} | |
if (buffer.length > 0) yield buffer; | |
buffer = []; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment