Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

Node.js 7 的 async await 终于来了,不过怎么觉得没什么用?

$
0
0
global.Promise = require('bluebird');
const co = Promise.coroutine;

const a = co(function* (abc) {
  console.time(abc);
  for (var i = 1; i < 10000; i++)
    yield Promise.delay(0);
  console.timeEnd(abc);
});

a('test1');

const b = async function(abc) {
  console.time(abc);
  for (var i = 1; i < 10000; i++)
    await Promise.delay(0);
  console.timeEnd(abc);
}

b('test2');

运行结果

test1: 13575.528ms
test2: 13819.517ms

速度基本一样,代码长度也基本一样,不过bluebird的corutine可以yield promise, 也可以yield generator function 没看出来 async await 的语法有什么优势?


Viewing all articles
Browse latest Browse all 14821

Trending Articles