Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Yield

 Yield

Lightning talk.
How Generators and Co can make asynchronous code look better.
Where to use it - from Co to Koa.

krzychukula

October 29, 2015
Tweet

More Decks by krzychukula

Other Decks in Programming

Transcript

  1. yield
    @krzychukula

    View full-size slide

  2. function(){
    getUsername(function(err, username){
    getUser(username, function(err, user){
    //use username and user
    })
    })
    }

    View full-size slide

  3. function(){
    getUsername()
    .then(function (username) {
    return [username, getUser(username)];
    })
    .spread(function (username, user) {
    //use username and user
    });
    }

    View full-size slide

  4. co(function *(){
    var username = yield getUsername()
    var user = yield getUser(username)
    //use username and user
    })

    View full-size slide

  5. co(function *(){
    var username = yield getUsername()
    var user = yield getUser(username)
    //use username and user
    })

    View full-size slide

  6. co(function *(){
    var username = “janek”
    var user = yield getUser(username)
    //use username and user
    })

    View full-size slide

  7. co(function *(){
    var username = “janek”
    var user = {id:”123”}
    //use username and user
    })

    View full-size slide

  8. var koa = require('koa');
    var app = koa();
    app.use(function *(){
    this.body = 'Hello World';
    });
    app.listen(3000);

    View full-size slide

  9. function *list() {
    var res = yield books.find({});
    this.body = res;
    }

    View full-size slide

  10. nvm install v4

    View full-size slide

  11. Play with it!

    View full-size slide

  12. Thanks!
    krzychukula.blogspot.com

    View full-size slide