3 ms·
Iterators and generators are powerful and can make code much cleaner/more declarative. Especially when used with async/await. If anyone is interested, I recent
by jondubois 8y ago
Iterators and generators are powerful and can make code much cleaner/more declarative. Especially when used with async/await.
If anyone is interested, I recently released a real-time WebSocket library (Node.js and front end) which only uses Async Iterators to stream data (no event listener callbacks):
https://hackernoon.com/getting-started-with-asyngular-bbe3dd1c716c https://hackernoon.com/getting-started-with-asyngular-bbe3dd...
Feedback welcome.
- sephoric 8y agoAs a cool example of the practicality of generators, my son and I wrote a generator in PICO-8 (Lua calls it coroutines) to animate moving the tiles in a 2048 game we made, using simple a for-loop to animate the tile's pixel coordinates, but yielding at the end of the loop body, so that we could let _update() and _draw() continue. This allowed the program to still be able to respond to key presses, which would "skip" to the end of an animation by fast-forwarding the coroutine until it was dead, and then create and start the next animation coroutine. I think when he saw that it was just a for-loop but it that kept "pausing" (yielding) at the end of each iteration, he finally understood generators.