Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've been playing with 'Co', the thunk / flow control framework TJ uses for Koa:

    #!/usr/bin/env node --harmony
    var fs = require('fs'),
      co = require('co'),
      thunkify = require('thunkify'),
      request = require('request'),
      readFile = thunkify(fs.readFile);
      get = thunkify(request.get);

    var log = console.log.bind(console);

    // Create a thunk, and run it immediately.
    co(function *(){
      try {
        var fileContents = yield readFile('myfile', 'utf8');
        log(fileContents);
        var yahoo = (yield get('http://yahoo.com'))[0];
        log(yahoo.statusCode)
      } catch(error) {
        log('Oh no, an error!')
        log(error.code)
      }
    })()
Async, inline, catching errors. It's nice.


Yeah, once you start using yield it's really hard to write JS without it. It's nice to get that keyword back.

You can start throwing that on mocha too, it's pretty sweet.


It is nightmare for v8 jit.


Who needs JIT on I/O bound tasks?


Any links for that?


v8 does not optimize blocks inside try catch (not too fresh http://www.html5rocks.com/en/tutorials/speed/v8/) and about generators and optimizations (http://wingolog.org/archives/2013/06/11/ecmascript-generator...)


it's a nightmare for crankshaft.


Yes, you are right. I forgot how they call own optimizer. (i wanted to point that people do not repeat blindly that peace of code)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: