Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MattiasBuelens/496fc1d37adb50a733edd43853f2f60e to your computer and use it in GitHub Desktop.
Save MattiasBuelens/496fc1d37adb50a733edd43853f2f60e to your computer and use it in GitHub Desktop.
ReadableStream async iterator polyfill
/**
* 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment