Skip to content

Instantly share code, notes, and snippets.

Last active November 24, 2020 17:59
What would you like to do?
Teeing an asynchronous iterator
function tee(asyncIterable) {
let source = asyncIterable[Symbol.asyncIterator]()
return [[], []].map((buffer, i, buffers) => ({
async next() {
if (0 in buffer) return buffer.shift()
let item = await
if (!item.done) buffers[1 - i].push(item)
return item
[Symbol.asyncIterator]() {
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment