Created
September 1, 2023 09:17
-
-
Save MattiasBuelens/496fc1d37adb50a733edd43853f2f60e to your computer and use it in GitHub Desktop.
ReadableStream async iterator polyfill
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
/** | |
* A polyfill for `ReadableStream.protototype[Symbol.asyncIterator]`, | |
* aligning as closely as possible to the specification. | |
* | |
* @see https://streams.spec.whatwg.org/#rs-asynciterator | |
* @see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#async_iteration | |
*/ | |
ReadableStream.prototype.values ??= function({ preventCancel = false } = {}) { | |
const reader = this.getReader(); | |
return { | |
async next() { | |
try { | |
const result = await reader.read(); | |
if (result.done) { | |
reader.releaseLock(); | |
} | |
return result; | |
} catch (e) { | |
reader.releaseLock(); | |
throw e; | |
} | |
}, | |
async return(value) { | |
if (!preventCancel) { | |
const cancelPromise = reader.cancel(value); | |
reader.releaseLock(); | |
await cancelPromise; | |
} else { | |
reader.releaseLock(); | |
} | |
return { done: true, value }; | |
}, | |
[Symbol.asyncIterator]() { | |
return this; | |
} | |
}; | |
}; | |
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values; |
I've subsequently posted an issue in the WebIDL github to confirm whether this behavior is intentional or an oversight: whatwg/webidl#1522.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MattiasBuelens important note though: it does not appear as though the incoming promise is awaited:
Chrome:

Firefox:

Given that information, the best typescript fix to align with existing behavior seems to be: