1. 2
  1. 1

    Generators have a more extensive array of capabilities than asynchronous functions, but are capable of replicating similar behavior. Implementing asynchronous programming in this way can increase the flexibility of your code.

    I’d be sad if I saw a PR where someone had used generators to replace an async/await function, without extremely good cause (which the article doesn’t go into, and I can’t think of one, and even if I could I’d try to find a way to not use generators). Talk about a great way to confuse your colleagues.

    Generators also suck to step through in a debugger if you’ve compiled them down to ES5.

    1. 2

      The article explicitly states that replacing async/await with generators is not something to be done in production, it’s simply a theoretical exercise to display some of the capabilities of generators. The benefits of generators come with working with infinite data streams and being able to easily close a generator, as canceling a promise is not always very simple.

      1. 1

        The article explicitly states that replacing async/await with generators is not something to be done in production, it’s simply a theoretical exercise to display some of the capabilities of generators

        To nitpick, that’s not how I read what it says, which is this:

        Note that this implementation is for demonstrating how generators can be used in place of async/await, and is not a production-ready design. It does not have error handling set up, nor does it have the ability to pass parameters into the yielded values. Though this method can add flexibility to your code, often async/await will be a better choice, since it abstracts implementation details away and lets you focus on writing productive code.

        That’s a little weaker than “replacing async/await with generators is not something to be done in production, it’s simply a theoretical exercise to display some of the capabilities of generators” 😀

        Anyway, it was a good article, well written, and thanks for submitting it to the site. Don’t take my complaining too seriously, I just wouldn’t want to see anyone replacing async/await (or promises) with their own generator-based implementation in real-world code without a really good reason.

        1. 2

          The way I read that paragraph is “this is for demonstration purposes only, this is not production level code, and you should probably just use async/await instead”, but it’s interesting to know that some people did not read it that way, so I’ll incorporate that knowledge into future writings.

          Generators were around for a small period before Async/await though, so people were experimenting with stuff like this. And thanks for the compliment! 🙂

    2. 1

      This is a good writeup, thank you!

      I don’t think I’ve ever really encountered generators in the wild with anywhere near the popularity or utility of Promises or async/await . Still, a very helpful writeup that helped me a lot. :)

      1. 2

        Glad you liked it! Generators are definitely not a drop in replacement for async/await, but they do have some unique features that it’s nice to be aware of.