0

I was having a hard time understanding ES2015 yield, specifically the fact that you can call an async function with sync style, and most of my question where solved by this answer https://stackoverflow.com/a/18904835/1540114

You can write async code in sync style inside them (e.g. generators) because they can yield. But you need a companion function that creates the generator, handles the callbacks and resumes the generator with a next call every time a callback fires.

Is there an official name for what he calls a "companion function", I saw codes using a "run" function and other using "spawn" ? And is there a native "companion function" or do we have to use a library ?

Community
  • 1
  • 1
Gatoyu
  • 642
  • 1
  • 6
  • 22
  • 3
    You'll need something like [co](https://www.npmjs.com/package/co) or [bluebird](https://www.npmjs.com/package/bluebird)'s [`Promise.coroutine()`](http://bluebirdjs.com/docs/api/promise.coroutine.html) function. There isn't a native function to wrap the generators for you. – idbehold Jun 08 '16 at 15:29
  • 1
    I'm often calling these "coroutine runners", because they, well, *run* the generator asynchronously. I've never heard them being named "companion". – Bergi Jun 08 '16 at 20:52

0 Answers0