现在在nodejs 0.10中使用ECAMScript 6,运行yield异步编程

npm install -g traceur

traceur --out out.js --script test.js

// test.js

function* f() {
    yield 100;
    console.log(100);
}

var m = f();
m.next();
m.next();

===> 编译

// out.js

var $__0 = $traceurRuntime.initGeneratorFunction(f);
function f() {
  return $traceurRuntime.createGeneratorInstance(function($ctx) {
    while (true)
      switch ($ctx.state) {
        case 0:
          $ctx.state = 2;
          return 100;
        case 2:
          $ctx.maybeThrow();
          $ctx.state = 4;
          break;
        case 4:
          console.log(100);
          $ctx.state = -2;
          break;
        default:
          return $ctx.end();
      }
  }, $__0, this);
}
var m = f();
m.next();
m.next();

你可能感兴趣的:(现在在nodejs 0.10中使用ECAMScript 6,运行yield异步编程)