CNode https://cnodejs.org/topic/5709cb2f94b38dcb3c09a7ac
提议 https://tc39.github.io/ecmascript-asyncawait/ 目前在 stage3, 没赶在 ES2016 即 ES7 的 deadline 之前达到 stage4, 所以只能赶下波 ES2017 了。 已经用上 babel 的可以不用往下看了~不是你们的菜~
async function foo(){
  let b = await bar();
  return b + 1;
}
但是只要满足一点就可以了,返回值是 Promise 类型,即可称之为 aysnc function. 例如
function bar(){
  return Promise.resolve(1);
}
// or
async function bar(){
  return 1;
}
这两种形式对于使用者 foo() 来说没有任何不同。
see https://tc39.github.io/ecmascript-asyncawait/#desugaring 在 async/await 语法糖之下是什么呢
async function <name>?<argumentlist><body>
=>
function <name>?<argumentlist>{ return spawn(function*() <body>, this); }
是有一个 spawn 函数来帮你运行一个 generator, 并返回一个 spawn 的返回值。 proposal 里给出了 spawn 的实现
function spawn(genF, self) {
    return new Promise(function(resolve, reject) {
        var gen = genF.call(self);
        function step(nextF) {
            var next;
            try {
                next = nextF();
            } catch(e) {
                // finished with failure, reject the promise
                reject(e);
                return;
            }
            if(next.done) {
                // finished with success, resolve the promise
                resolve(next.value);
                return;
            }
            // not finished, chain off the yielded promise and `step` again
            Promise.resolve(next.value).then(function(v) {
                step(function() { return gen.next(v); });
            }, function(e) {
                step(function() { return gen.throw(e); });
            });
        }
        step(function() { return gen.next(undefined); });
    });
}
可以去看 co 的源码,跟 co 差不多, co 要多一些将其他非 promise 值转为 promise 的过程。 所以可以这样使用
function foo(){
  return co(function*(){
    let b = yield bar();
	return b + 1;
  });
}
这个 foo 跟 前面的 async function foo() { ... } 对于使用者来说没有任何区别, (foo.length 除外, 待会说).
于是可以使用一个方法去生成这样一个用 generator 写的 aysnc function
// 使用 co.wrap
let foo = co.wrap(function*(){
  let b = yield bar();
  return b + 1;
});
// 使用 bluebird.coroutine
let foo = bluebird.coroutine(function*(){
  let b = yield bar();
  return b + 1;
});
所以推荐使用 co.wrap & bluebird.coroutine 或者其他工具将你的函数封装成一个可以返回 Promise 的广义 async function 。 并不需要等到 aysnc/await 变成 native. 将来迁移到 async/await 只需要修改这个定义的地方即可(如 bar), 调用者(如 foo)并不需要知晓你的改变。
async funtion foo1(n){
  let b = await bar();
  return b + n;
}
let foo2 = co.wrap(function*(n){
  let b = yield bar();
  return b + n;
});
这两种形式,抛开一个是 function declaration, 一个是 variable declaration的差异,还有一点就是
err, req, res, next, 是 4 个才给你传 err就跪了。
https://cnodejs.org/topic/56ab1c0526d02fc6626bb383 裸奔着用 generator, 那样使用者必须使用 co, 绑死使用者是很危险的哦. see
其他使用 promise.then.then.then.then ... 赶紧向这边迁移吧~
个人观点,不对请指正。谢谢。
|      1fds      2016-04-11 05:58:31 +08:00 在用 TypeScript 自带的 async/await ,确实挺方便的。只是目前只支持转到 es6 ,直接喂给 node 需要手动少写些新特性的代码。 | 
|      2welefen      2016-04-12 12:08:53 +08:00 @fds TS 里用 async/await ,编译模式就要选 ES6 ,然后 class 等 ES6 特性就不编译了。 最后只好用 tsc + babel 编译搞定 | 
|      4fds      2016-04-12 17:16:31 +08:00 @welefen 现在 atom 编辑器支持 ts 的 compile on save ,加上 babel 就不方便了,所以只能妥协中 | 
|      6welefen      2016-04-12 17:18:35 +08:00 @fds 并且现在使用 Babel 编译还有一个好处是,有些编译后的代码性能要优于现在 V8 支持的代码(因为现在 V8 主要还是在实现 ES6 特性,还没做什么优化) | 
|  |      7magicdawn OP | 
|      8fds      2016-04-13 10:41:53 +08:00 @welefen 哦,这样呀,受教了。我当时给 babel 配 spread operator 支持时换了好几个 plugin 才能正确处理,担心有问题,就没有继续使用了。 说到优化。我特别想吐槽下 node 的速度, v4 比 v0.10 慢了快一倍,线上同样的代码。 @magicdawn 我是从 github.com/caolan/async 直接跳到 es7 的 async/await 的,所以自己没用过 generator ;p | 
|      9welefen      2016-04-13 11:38:09 +08:00 | 
|  |      10frogcjn      2016-05-16 15:19:16 +08:00 @welefen @fds 试了一下, node 6.1.0 + express 4.13.4 + TypeScript 1.8, 输出选择 es6 , class , async func 可以共存也可以直接用。 https://github.com/Microsoft/typescript-build2016-demos TypeScript 2 也快出了,支持 async func 输出到 ES5 |