Skip to content

Instantly share code, notes, and snippets.

@jollytoad
Last active September 29, 2023 10:26
Show Gist options
  • Save jollytoad/5da9d69756624ff37752dceef29aff88 to your computer and use it in GitHub Desktop.
Save jollytoad/5da9d69756624ff37752dceef29aff88 to your computer and use it in GitHub Desktop.
Polyfill for ReadableStream.prototype[@@asyncIterator]
/**
* @template T
* @this {ReadableStream<T>}
*/
async function* readableStreamIterator() {
const reader = this.getReader();
try {
let done, value;
do {
({ done, value } = await reader.read());
if (value !== undefined) {
yield value;
}
} while (!done);
} finally {
reader.releaseLock();
}
}
ReadableStream.prototype[Symbol.asyncIterator] ??= readableStreamIterator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment