Difference between async/await and ES6 yield with generators

前端 未结 6 778
一个人的身影
一个人的身影 2021-01-30 00:03

I was just reading this fantastic article «Generators» and it clearly highlights this function, which is a helper function for handling generator functions:



        
6条回答
  •  离开以前
    2021-01-30 00:40

    tl;dr

    Use async/await 99% of the time over generators. Why?

    1. async/await directly replaces the most common workflow of promise chains allowing code to be declared as if it was synchronous, dramatically simplifying it.

    2. Generators abstract the use case where you would call a series of async-operations that depend on each other and eventually will be in a "done" state. The most simple example would be paging through results that eventually return the last set but you would only call a page as needed, not immediately in succession.

    3. async/await is actually an abstraction built on top of generators to make working with promises easier.

    See very in-depth Explanation of Async/Await vs. Generators

提交回复
热议问题