Use JavaScript (ES6) generators with Promises to handle async flows

Share this video with your friends

Send Tweet

With one tiny utility function we’ll unlock the full power of generators to make them work well with Promises and thus be the perfect weapon for asynchronous flows in our apps.

Pawel Grzybek
Pawel Grzybek
~ 8 years ago

Fantastic course! Short and easy to watch / understand with a morning coffee. The last lesson enlightened me a lot — finally I understand the inner logic of async / await functions which in my eyes are essentially a combination of promises and generators. Thanks a ton for this course, looking forward for more your resources! Have a great day.

Max Stoiber
Max Stoiber(instructor)
~ 8 years ago

Thank you for the kind words, glad you enjoyed it 😊

Paul Vincent Beigang
Paul Vincent Beigang
~ 8 years ago

How does a comparison between generators and observables look like?

Raphi
Raphi
~ 8 years ago

Hi Max,

The videos were informative. I'm wondering however, in your last video where you use generators and Promises together to fetch some data, wouldn't it be much simpler to just use Promises in that fetchQuote generator (or rather, fetchQuote function)?

I fail to see what the generator+Promises adds in this case. Perhaps you were just trying to give an idea of the capabilities but used a not so stellar example.

Anshuman
Anshuman
~ 8 years ago

A Promise-based implementation of fetchQuote() would consist of a chain of .then() calls. While there is nothing wrong with that, using yield instead allows you to write code that looks almost like a synchronous version of fetchQuote() would.

Imagine that fetch() is synchronous and picture the body of fetchQuote() without the yield keyword.

Victor Hazbun
Victor Hazbun
~ 7 years ago

THIS IS AMAZING!

Alex
Alex
~ 7 years ago

good course last lesson could be done with await and it would work, it would be good if you would incorporate side effect on yields to show why this could be better used.

redux-saga for examples provides great use cases for generators.

Austin Hunt
Austin Hunt
~ 5 years ago

Doesn't async await make this no longer very useful?

Niv B
Niv B
~ 5 years ago

@Austin In did, but even now there are some enterprises and system codes still writing like.

Mova.Club
Mova.Club
~ 3 years ago

Many thanks for your great job!

Amir Mousavi
Amir Mousavi
~ 3 years ago

Fantastic one!

~ 3 years ago

Thank you for the course Max, I enjoyed the videos. But out of curiosity - does this have any benefits over async..await? (I understand the videos are quite old but still wanted to know your thoughts on this)

Tre' Codez
Tre' Codez
~ 2 years ago

Can use this url for quotes: https://api.quotable.io/random