Skip to content

AsyncReader.Class.AsyncIteratorReader

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

tedious-plus / AsyncReader / AsyncIteratorReader

Class: AsyncIteratorReader<T>

Defined in: AsyncReader.ts:70

A reader for the values of an async iterable, for procedural processing (as an alternative to a for await loop).

Usage:

await using reader = new AsyncIteratorReader(iterable);
const firstValue = await reader.read();
const secondValue = await reader.read();

Behavior:

  • read() returns undefined at the end of the iteration. If undefined can be a legitimate value of the iterable, use readResult() instead.
  • After the end has been reached, after an error or after close(), read() returns undefined without accessing the underlying iterator again.
  • If the underlying iterator fails, the promise returned by read() is rejected. The iteration is then considered finished, as in a for await loop.
  • close() terminates the iteration early by calling return() on the underlying iterator. This has the same effect as exiting a for await loop early. close() is called implicitly at the end of the scope of an await using declaration.
  • read() must not be called again before the promise of the previous call has settled.
  • The remaining values can also be consumed with a for await loop over the reader. Exiting that loop early closes the reader.

Type Parameters

T

T

Implements

Constructors

Constructor

new AsyncIteratorReader<T>(iterable): AsyncIteratorReader<T>;

Defined in: AsyncReader.ts:82

Creates a reader for an async iterable.

Parameters

iterable

AsyncIterable<T>

The async iterable whose values are to be read.

Returns

AsyncIteratorReader<T>

Accessors

done

Get Signature

get done(): boolean;

Defined in: AsyncReader.ts:89

Returns true if the iteration has finished, i.e. the end has been reached, an error has occurred or the reader has been closed.

Returns

boolean

Methods

[asyncDispose]()

[asyncDispose](): Promise<void>;

Defined in: AsyncReader.ts:139

Closes the reader. Called implicitly at the end of the scope of an await using declaration.

Returns

Promise<void>

Implementation of

AsyncDisposable.[asyncDispose]


[asyncIterator]()

[asyncIterator](): AsyncGenerator<T, void, undefined>;

Defined in: AsyncReader.ts:146

Allows the remaining values to be consumed with a for await loop. Exiting the loop early closes the reader.

Returns

AsyncGenerator<T, void, undefined>


close()

close(): Promise<void>;

Defined in: AsyncReader.ts:130

Terminates the iteration early. Has no effect if the iteration has already finished, so it can be called multiple times (idempotent).

Calls return() on the underlying iterator, if available, and waits for its completion.

Returns

Promise<void>

Implementation of

AsyncReader.close


read()

read(): Promise<T | undefined>;

Defined in: AsyncReader.ts:98

Reads the next value.

Returns

Promise<T | undefined>

A promise for the next value, or undefined if the iteration has finished.

Implementation of

AsyncReader.read


readResult()

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

Defined in: AsyncReader.ts:106

Reads the next value and returns it as an IteratorResult. Use this method instead of read() if undefined can be a legitimate value of the iterable.

Returns

Promise<IteratorResult<T, undefined>>

Clone this wiki locally