News for hackers. Stuff that matters.
Re:Internals of Async / Await in JavaScript by masklinn on Saturday September 02, 2023 @06:52AM (#37360301)
Generators are not used much in the JS ecosystem for some reason, but they're used a lot in Python.

The most common application is iteration (lazy producers or transformers), but they're also used for their ability to suspend and resume function execution (interruptible functions)

- pytest fixtures use generators to setup, allow the test to run (yielding a value or not), and teardown

- contextlib.contextmanager uses generators for similar purposes of entering a context, allowing the wrapped code to run, and then exiting the context

They can also act as cheap state managers, especially with the ability to inject data back into the generator, so they can be used to implement a multi-step function where the user needs multiple points of interaction, without needing a bunch of callbacks, or complicated state management (especially without the benefit of sum types or static state machines)

Re:Internals of Async / Await in JavaScript by athanagor2 on Saturday September 02, 2023 @07:51AM (#37360631)
I used generators in the way you described in your last paragraph. And I added the possibility to "replay" the generators with the previous inputs except the last one. It provides a cheap way to have a kind of multi-step input process with cancellation.