The context used to manage the iteration over the items.
A new Iterator instance
let idx = -1;
let theValues = [ 5, 10, 15, 20, 25, 30 ];
function getNextFn() {
idx++;
let isDone = idx >= theValues.length;
if (!isDone) {
// this is passed as the current iterator
// so you can directly assign the next "value" that will be returned
this.v = theValues[idx];
}
return isDone;
}
let theIterator = createIterator<number>({ n: getNextFn });
let values: number[] = [];
iterForOf(theIterator, (value) => {
values.push(value);
});
// Values: [5, 10, 15, 20, 25, 30 ]
Create an iterator which conforms to the
Iterator
protocol, it uses the providedctx
to managed moving to thenext
.