Comment by geon
4 hours ago
In typescript/js, you can iterate over async generators with for-await-of. Very ergonomic.
This article sums it up pretty well: https://medium.com/@bhagyarana80/why-async-generators-were-t...
In nodejs, all streams are async iterables, so you can iterate over them. Really nice for handling stuff like connections to a server, or messages on a socket.
I’ve use generators to code ui “sagas”. You await async events like clicks etc, and yield the appropriate state updates. A sub-dialog can be implemented as a separate generator, and the main dialog generator can open it by with the yield* syntax to iterate over and pass on all the events until the generator returns. The return value would be the result of the dialog.
Super nifty.
No comments yet
Contribute on Hacker News ↗