DEV Community

machy44
machy44

Posted on

When do you use generator based library for async programming instead of the async/await?

When I have to do some async programming I always choose the async/await syntax. But today I have read about co lib which is based on generators. What are the pros and cons of both approaches?

Could you give me an example in which situations are generators better choice for async programming than async/await?

Top comments (2)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

co seems old -- 5 years ago.

Async paradigm has criticisms -- namely, watching what's inside, and cancelling.

Observable is a solution to this. A biggest player in this is RxJS. The pattern is not limited to JavaScript. (Not sure if the library started from Java, actually.)

Triggering changes from outside is possible for observables too.

Collapse
 
machy44 profile image
machy44

Aha, now I realized. You cannot pause async function but you can do it with generators. I did not think about that. Thank you for your reply. You were very helpful. I have never worked with RxJS and I will certainly take a look.