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
    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
    LeonineKing1199
    @LeonineKing1199
    Um, not that I'm aware of
    Mark Moore
    @mmoo9154
    np
    LeonineKing1199
    @LeonineKing1199
    There's a C++ gitter that I'm a part of too
    So if you ever feel like chatting about C++, there is a gitter for it
    Barney Carroll
    @barneycarroll
    Hi folks!
    I've been working with promises for ages, and
    in the last year I've been using async / await with some delight
    Something I didn't get early on in my playing with async / await was a fair bit of criticism that deemed the proposal redundant since we already had generators
    This was because I'd never really grokked generators, they were too broad in their use cases and I'd never come up with a scenario where I'd used them to significant effect
    Barney Carroll
    @barneycarroll
    However recently I've discovered the power of splitting low level logic into subroutines and I've realised just how useful generators can be
    co was always cited as the empowering toolkit that made generators shine, so I thought I'd come back and read a bit in the hope of getting some more ideas of the possibility space of generators
    Barney Carroll
    @barneycarroll
    And I'm surprised to see that the README says generators and generator functions are now only consumable as yieldables as a backward compatibility measure, with the recommendation being that people use promises instead
    This strikes me as odd, since it suggests that in the modern spec world, co is essentially relegating itself to async / await with special unwrapping behaviour
    Does anyone have any insight as to the rationale behind "avoid yielding generators"?
    LeonineKing1199
    @LeonineKing1199
    I'm not sure I understand the logic as well :P