Identifies the type that will be returned by the iterator
The context used to manage the iteration over the items.
A new IterableIterator instance
let idx = -1;
let theValues = [ 5, 10, 15, 20, 25, 30 ];
let theIterator = createIterableIterator<number>({
n: function() {
idx++;
let isDone = idx >= theValues.length;
if (!isDone) {
this.v = theValues[idx];
}
return isDone;
}
});
// Can be consumed as an iterator
theIterator.next(); // { value: 5, done: false }
// Or used in a for...of loop (Iterable protocol)
let values: number[] = [];
for (const value of theIterator) {
values.push(value);
}
// Values: [10, 15, 20, 25, 30]
Create an iterable iterator which conforms to both the
IteratorandIterableprotocols, it uses the providedctxto manage moving to thenextvalue and can be used directly infor...ofloops or consumed via.next()calls.The returned object satisfies both protocols: its
[Symbol.iterator]()method returns itself, making it usable anywhere anIterableis expected, while also exposing thenext()method of theIteratorprotocol.