Difference between async/await and ES6 yield with generators

前端 未结 6 779
一个人的身影
一个人的身影 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:31

    what the heck is the difference between the await keyword and the yield keyword?

    The await keyword is only to be used in async functions, while the yield keyword is only to be used in generator function*s. And those are obviously different as well - the one returns promises, the other returns generators.

    Does await always turn something into a promise, whereas yield makes no such guarantee?

    Yes, await will call Promise.resolve on the awaited value.

    yield just yields the value outside of the generator.

提交回复
热议问题