📎 Webclip
Why would anyone need JavaScript generator functions?
Generators are presented as an odd, low-level JavaScript feature that can be hard to read in isolation, but useful in specific cases. The article focuses on three uses: lazy processing of data, building infinite iterators, and passing messages between functions.
Reading notes#
- Generators are described as a low-level construct that works like a tool for building other tools.
- Their syntax is presented as unusual because of starred function definitions and the
yieldkeyword. - Lazy iterators are introduced as the most immediate use case.
- The Tim Tam example shows how generator-based pipelines can process items one at a time instead of all at once.
- Utility functions such as
map,take, andforEachare used to make generator pipelines work. - Laziness is said to help with large data sets by loading one item at a time into memory.
- Generators are also used to create infinite iterators, such as repeating a value or generating natural numbers.
- The article uses
scanl,filter,pop,drop, andsieveto build a prime-number generator. - It says generators and iterators are mutable, so consuming one item changes the sequence that remains.
- The article notes that iterator helpers are not built in yet, but libraries such as Itertools and IxJS can fill the gap.
- Message passing is presented as another major use, especially for emulating
async/awaitwith generators. - It shows a generator-based
asyncDowrapper that resolves yielded promises step by step. - The article also shows how generator message passing can simplify Either-based error handling.
- It ends by saying generators are useful for efficient data processing, infinite sequences, and communication between functions.
