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
    Oh I see, these are keys in an already existing object!
    You might have to forgive me, my OOP JS is incredibly weak.
    In fact, I don't do much OOP, period.
    But in your case, I'd say that you can just create a method and then call return [this.getAllLabels(), this.getAllButtonTexts]
    If that's jQuery though, you should cache your selectors
    Mark Moore
    @mmoo9154
    Is this a good place for a n00b to co? If not, can someone point me to a good place to start? Thx.
    LeonineKing1199
    @LeonineKing1199
    This the place to go if you're a newb
    :P
    Mark Moore
    @mmoo9154
    I'm a bit down a rathole trying to connect to mongo with a Nod.js app in MSVC 2017 RC... As I was crusing through the mongodb node.js driver docs at http://mongodb.github.io/node-mongodb-native/2.2/reference/ecmascript6/connecting/ I came across co for the first time.
    LeonineKing1199
    @LeonineKing1199
    Oh nice!
    Mark Moore
    @mmoo9154
    Looks interesting, but I don't see a lot of docs on co. Is there a tutorial or a dev guide somewhere?
    LeonineKing1199
    @LeonineKing1199
    There should be a github page with basic examples... Lemme check real fast
    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