DeferredSequence.resolve()method
Resolve the current deferred in the sequence with a value.
resolve(value: T): void
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. |
| Property | Type | |
|---|---|---|
.promise | Promise<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.
Deferred so the next result can be set.Promise so the next result can be awaited.AsyncIterable so values can be iterated over using for await...ofresolve(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.
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.
Because DeferredSequence implements Promise, you can await it for a single value without setting up a loop:
const next = await seq; // resolves when resolve() is next calledconst seq = new DeferredSequence<number>();
(async () => { for await (const n of seq) console.log(n); })();
seq.resolve(1);
seq.resolve(2);
seq.done();Resolve the current deferred in the sequence with a value.
resolve(value: T): void
Reject the current deferred in the sequence with an error.
reject(reason: unknown): void
Signal that the sequence is done, causing any active for await loops to exit cleanly.
done(value?: R | undefined): void
Cancel the current resolution or rejection.
cancel(): void
Resolve the current deferred from a sequence of values.
through(sequence: AsyncIterable<T>): AsyncIterator<T>
next(_next?: N | undefined): Promise<IteratorResult<T, R | undefined>>
then(onNext?: Nullable<(v: T) => X | PromiseLike<X>>, onError?: Nullable<(r: unknown) => Y | PromiseLike<Y>>): Promise<X | Y>
catch(onError: (r: unknown) => Y | PromiseLike<Y>): Promise<T | Y>
finally(onFinally: () => void): Promise<T>