Ideas for Generator Functions
async function* foo() {
yield 1;
yield 2;
}
(async function () {
for await (const num of foo()) {
console.log(num);
// Expected output: 1
break; // Closes iterator, triggers return
}
})();
Conecpts
forward only evaluation
- memory friendly
- infinite
Creating your own iterators
Chunking
function* chunks(arr: Array<Bit>, n = 10): Generator<Array<Bit>, void> {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n);
}
}
Lazy Loading
Pagination
Animations
Infinite Series
Data that comes in constantly and will never end - for example live updates to data that happens once a minute.
Input Series
Input yeilded similar to the command line - write a prompt and wait for input
Want to read more? Check out more posts below!