Last active
September 29, 2023 10:26
-
-
Save jollytoad/5da9d69756624ff37752dceef29aff88 to your computer and use it in GitHub Desktop.
Polyfill for ReadableStream.prototype[@@asyncIterator]
This file contains 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
/** | |
* @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
See https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#async_iteration