Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    LeonineKing1199
    @LeonineKing1199
    Yeah, this has examples on its usage
    Basically with co, you yield out a Promise and you profit :P
    Mark Moore
    @mmoo9154
    I saw the examples, but they didn't seem to apply to the code on the Mongo Node.js Driver page.
    LeonineKing1199
    @LeonineKing1199

    var db = yield MongoClient.connect(url);

    This line?

    MongoClient has a connect method which takes a url and then returns a Promise
    Mark Moore
    @mmoo9154
    What does co(function*(){...}); do with the passed in function/iterator?
    LeonineKing1199
    @LeonineKing1199
    Ah, brilliant.
    So...
    co takes a generator and iterates it for you
    Basically, you give co a generator and everytime you yield a Promise, co will call it.next() with the resolved value
    Mark Moore
    @mmoo9154
    I kinda get promises...
    LeonineKing1199
    @LeonineKing1199
    const co = function(genFn) {
      const it = genFn();
    
      const promise = it.next();
     promise.then((resolvedValue) => it.next());
    };
    That's kind of how co works
    Understanding how co is implemented is interesting for sure
    It requires good knowledge of both Promises and generators
    To use co, just know that you should yield a Promise
    Mark Moore
    @mmoo9154
    So, where does the connect promise yield to?
    LeonineKing1199
    @LeonineKing1199
    Generators are hecka cool because they represent an isolated executable context
    Generators sort of live outside of the main JS event loop
    So co is a function that runs in your land and will iterator your generator. Part of iteration in JS means that you can feed values back into your generators
    Mark Moore
    @mmoo9154
    Maybe the idea is that control resumes just below the var db = yield MongoClient.connect(url); line...
    LeonineKing1199
    @LeonineKing1199
    co takes advantage of this. When you yield a Promise to co, co will take that Promise and say, Hey, when this Promise resolves, feed the value back into the generator
    Mark Moore
    @mmoo9154
    Then I can use db
    LeonineKing1199
    @LeonineKing1199
    Inside a generator, execution stops at any yield
    Execution only resumes when the iterator's .next() is called
    So when you type yield, you're actually suspending execution inside the generator
    Mark Moore
    @mmoo9154
    Execution stops (yields) at any yield... But, on the next call to the iterator, execution resumes just after the yield. Yes?
    LeonineKing1199
    @LeonineKing1199
    Yup
    You can yield almost anywhere too, it's crazy
    Mark Moore
    @mmoo9154
    Now I kinda get what's going on.
    LeonineKing1199
    @LeonineKing1199
    Generators in JS are quite the mindfuck if you've never seen anything like it before
    Yeah
    In essence, a generator is just an executable region of memory that can be suspended and called into
    Generators are simplified coroutines
    So google "coroutines" if you really wanna see how crazy this stuff can be.
    Mark Moore
    @mmoo9154
    I have. I've done a lot of multithreaded coding, but mostly in c++. I get what's going on. Really appreciate your patience.
    LeonineKing1199
    @LeonineKing1199
    Yeah, JS iterators != C++ iterators, that's for sure
    Boost has coroutines, actually
    Mark Moore
    @mmoo9154
    Maybe add that tip to the co doc on npm. ;-) "google "coroutines" if you really wanna see how crazy this stuff can be."
    LeonineKing1199
    @LeonineKing1199
    But they're crazy to implement in C++
    Ha
    TJ's not big on updating co
    co is supposed to die when async/await are released officially
    Mark Moore
    @mmoo9154
    ahhh...
    LeonineKing1199
    @LeonineKing1199
    Yeah, if you look on the github issues for co, you'll see him specifically deny requests for expanding the module
    Mark Moore
    @mmoo9154
    Well, thanks again!
    LeonineKing1199
    @LeonineKing1199
    No problem!
    I'm like the only one here. But I'm allergic to work so I can answer any questions you may have :)
    Mark Moore
    @mmoo9154
    Is there a way to save this transcript? I haven't used gitter before just now