Skip to content

AsyncReader.Class.AsyncReaderIterator

Christian d'Heureuse edited this page Oct 28, 2026 · 1 revision

tedious-plus / AsyncReader / AsyncReaderIterator

Class: AsyncReaderIterator<T>

Defined in: AsyncReader.ts:185

An async iterator for the values of an AsyncReader.

The object is both the iterable and the iterator (like an async generator object), so it can be used directly in a for await loop. It is single-pass: a second loop continues where the first one stopped.

Usage:

for await (const value of new AsyncReaderIterator(reader)) {
   console.log(value);
}

Behavior:

  • The iteration ends when read() returns undefined.
  • If read() fails, the promise returned by next() is rejected, i.e. the error is thrown within the for await loop. The iteration is then considered finished.
  • The reader is closed exactly once by this object, however the iteration ends: at the end of the values, after an error of read(), or when the for await loop is exited early (by break, return or an exception in the loop body), in which case return() is called implicitly. Since close() must be idempotent (see AsyncReader.close), the reader may also be closed elsewhere, e.g. by an await using declaration.
  • next() must not be called again before the promise of the previous call has settled. A for await loop never does that.

Type Parameters

T

T

Implements

  • AsyncIterator<T, undefined>
  • AsyncIterable<T>
  • AsyncDisposable

Constructors

Constructor

new AsyncReaderIterator<T>(reader): AsyncReaderIterator<T>;

Defined in: AsyncReader.ts:196

Creates an async iterator for a reader.

Parameters

reader

AsyncReader<T>

The reader whose values are to be iterated.

Returns

AsyncReaderIterator<T>

Methods

[asyncDispose]()

[asyncDispose](): Promise<void>;

Defined in: AsyncReader.ts:241

Terminates the iteration. Called implicitly at the end of the scope of an await using declaration.

Returns

Promise<void>

Implementation of

AsyncDisposable.[asyncDispose]


[asyncIterator]()

[asyncIterator](): this;

Defined in: AsyncReader.ts:235

Returns this object, so that it can be used in a for await loop.

Returns

this

Implementation of

AsyncIterable.[asyncIterator]


next()

next(): Promise<IteratorResult<T, undefined>>;

Defined in: AsyncReader.ts:202

Returns the next value of the reader as an IteratorResult.

Returns

Promise<IteratorResult<T, undefined>>

Implementation of

AsyncIterator.next


return()

return(): Promise<IteratorResult<T, undefined>>;

Defined in: AsyncReader.ts:226

Terminates the iteration early and closes the reader. Has no effect if the iteration has already finished, because the reader has then already been closed. Called implicitly when a for await loop is exited early.

Returns

Promise<IteratorResult<T, undefined>>

Implementation of

AsyncIterator.return

Clone this wiki locally