DeferredSequenceclass

new DeferredSequence<T, R, N>()
Return
DeferredSequence<T, R, N>
Deferred sequence of values that can be async iterated and new values can be published.
PropertyType
.promisePromise<T>
Next promise to be resolved/rejected with the upcoming value. required readonly

Deferred sequence of values that can be async iterated and new values can be published.

  • Implements Deferred so the next result can be set.
  • Implements Promise so the next result can be awaited.
  • Implements AsyncIterable so values can be iterated over using for await...of
  • Call resolve(value) to publish the next value, reject(reason?) to publish an error, or done(value?) to signal completion.

A sequence that external code drives on demand. Call resolve(value) to publish the next value to every active iterator, reject(reason) to publish an error, or done() to end iteration cleanly. Calling cancel() discards a queued resolution without advancing iterators.

DeferredSequence is also a Promise, so you can await it directly to get just the next value without a loop. It is the primitive shelving/store uses internally to broadcast changes.

Usage

Publish and subscribe

ts
import { DeferredSequence } from "shelving/sequence";

const seq = new DeferredSequence<string>();

// Produce values from anywhere:
setInterval(() => seq.resolve("ping"), 1000);

// Consume in one or more places simultaneously:
for await (const msg of seq) {
  console.log(msg); // "ping" every second
}

Multiple concurrent iterators all advance together — a single resolve() delivers to every one.

Await the next value

Because DeferredSequence implements Promise, you can await it for a single value without setting up a loop:

ts
const next = await seq; // resolves when resolve() is next called

Examples

const seq = new DeferredSequence<number>();
	(async () => { for await (const n of seq) console.log(n); })();
	seq.resolve(1);
	seq.resolve(2);
	seq.done();

Methods

Go

DeferredSequence.resolve()method

Resolve the current deferred in the sequence with a value.

resolve(value: T): void
Go

DeferredSequence.reject()method

Reject the current deferred in the sequence with an error.

reject(reason: unknown): void
Go

DeferredSequence.done()method

Signal that the sequence is done, causing any active for await loops to exit cleanly.

done(value?: R | undefined): void
Go

DeferredSequence.cancel()method

Cancel the current resolution or rejection.

cancel(): void
Go

DeferredSequence.through()method

Resolve the current deferred from a sequence of values.

through(sequence: AsyncIterable<T>): AsyncIterator<T>
Go

DeferredSequence.next()method

next(_next?: N | undefined): Promise<IteratorResult<T, R | undefined>>
Go

DeferredSequence.then()method

then(onNext?: Nullable<(v: T) => X | PromiseLike<X>>, onError?: Nullable<(r: unknown) => Y | PromiseLike<Y>>): Promise<X | Y>
Go

DeferredSequence.catch()method

catch(onError: (r: unknown) => Y | PromiseLike<Y>): Promise<T | Y>
Go

DeferredSequence.finally()method

finally(onFinally: () => void): Promise<T>