Skip to content

Async iterator promises are not resolved after stream end #387

Closed
@reconbot

Description

@reconbot

Given the following code, any promises for iterator results after the stream has ended will never resolve.

const a = new stream.PassThrough()
const b = a[Symbol.asyncIterator]()
const c = b.next()
const d = b.next()
a.end()
await c // { done: true, value: null }
await d // never resolves

if you repeat the same process for a generator function

const a = (async function* (){})()
const b = a[Symbol.asyncIterator]()
const c = b.next()
const d = b.next()
a.end()
await c // { value: undefined, done: true }
await d // { value: undefined, done: true }

Any promises from calls to next from an exhausted async generator function's iterator will immediately resolve as done: true.

I noticed this difference when trying to perform parallel async transforms on the values from a stream that resolved in a non deterministic order. I rely on all calls to next after the source has been exhausted to resolve, and since they don't for streams it hung forever.

I'm unsure of if this is defined in the async iterator spec, but it does seem like it might want to match behaviors of async generator functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions