Sequence.next()method
Get a next value from this async iterator.
next(value?: N | undefined): Promise<IteratorResult<T, R | undefined>>
new Sequence<T, R, N>()
| Return | |
|---|---|
Sequence<T, R, N> | Sequence of values designed to be extended that implements the full async generator protocol. |
Sequence of values designed to be extended that implements the full async generator protocol.
Note: while this class implements the iterator protocol, it is not a generator.
next() will always return done: trueThe abstract base class for every sequence type. Sequence<T, R, N> implements AsyncIterator, AsyncIterable, and AsyncDisposable at once, so a single object can be iterated with for await...of, passed around as an iterable, and disposed with await using.
Unlike a generator, a Sequence places no one-shot constraint on iteration — throwing does not permanently end it. Subclasses implement the abstract next() method; return() and throw() have generator-like defaults that subclasses can override for cleanup.
Sequence is not used directly — extend it, or use a built-in subclass (DeferredSequence, ThroughSequence, LazySequence, InspectSequence). A minimal subclass only needs next():
import { Sequence } from "shelving/sequence";
class CountdownSequence extends Sequence<number, void, void> {
constructor(private n: number) { super(); }
async next() {
return this.n > 0
? { done: false as const, value: this.n-- }
: { done: true as const, value: undefined };
}
}class NumberSequence extends Sequence<number, void, void> {
async next() { return { done: false, value: Math.random() }; }
}Get a next value from this async iterator.
next(value?: N | undefined): Promise<IteratorResult<T, R | undefined>>
Finish iteration and optionally provide the iterator's final return value.
return(value?: R | undefined | PromiseLike<R | undefined>): Promise<IteratorResult<T, R | undefined>>
Throw an error into this iterator.
throw(reason?: unknown): Promise<IteratorResult<T, R | undefined>>