本文地址 http://www.cnblogs.com/jasonxuli/p/6047590.html

nodejs 7.0.0 已经支持使用 --harmony-async-await 选项来开启async 和 await功能。

在我看来,yield 和 async-await 都是在特定范围内实现了阻塞;从这方面来看,await 相当于在阻塞结合异步调用上前进了一步。

使用async前缀定义的function中可以使用await来等待Promise完成(promise.resolve() 或 promise.reject()), 原生Promise或者第三方Promise都可以。

"use strict";

console.log(process.version);

var Promise = require('bluebird');
var requestP = Promise.promisify(require('request')); async function testAsync(){
try{
return await requestP('http://www.baidu.com');
}catch(e){
console.log('error', e);
}
} var b = testAsync();
b.then(function(r){
console.log('then');
console.log(r.body);
}); console.log('done');

node.exe --harmony-async-await test.js

console结果:

v7.0.0
done
then
<!DOCTYPE html><!--STATUS OK-->
<html>
<head>

......

采用await,可以比较容易处理某些Promise必须结合循环的情况,比如:

async getStream(){

var result = '';

var chunk = await getChunk();

while (chunk.done == false){

result += chunck.data;

chunk = await getChunk();

}

}

比较起来,原生Promise看起来样子有些臃肿,而且无法显示错误信息的stack trace;倒是bluebird的promise的stack trace做的不错:

原生:

"use strict";

console.log(process.version);

Promise.resolve('aaa').then(function(){
throw new Error('error message');
}) console.log('done');

结果:

v7.0.0
(node:7460) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: error message
(node:7460) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
done

bluebird:

"use strict";

console.log(process.version);

var promise = require('bluebird');

promise.resolve('aaa').then(function(){
throw new Error('error message');
}) console.log('done');

结果:

v7.0.0
done
Unhandled rejection Error: error message
at f:\test\test2\test.js:49:11
at tryCatcher (F:\nodist\bin\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (F:\nodist\bin\node_modules\bluebird\js\release\promise.js:510:31)
at Promise._settlePromise (F:\nodist\bin\node_modules\bluebird\js\release\promise.js:567:18)
at Promise._settlePromiseCtx (F:\nodist\bin\node_modules\bluebird\js\release\promise.js:604:10)
at Async._drainQueue (F:\nodist\bin\node_modules\bluebird\js\release\async.js:143:12)
at Async._drainQueues (F:\nodist\bin\node_modules\bluebird\js\release\async.js:148:10)
at Immediate.Async.drainQueues (F:\nodist\bin\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:637:20)
at tryOnImmediate (timers.js:610:5)
at processImmediate [as _immediateCallback] (timers.js:582:5)

references:

https://blog.risingstack.com/async-await-node-js-7-nightly/

https://developers.google.com/web/fundamentals/getting-started/primers/async-functions

nodejs7.0 试用 async await的更多相关文章

  1. VS2010 + C#4.0使用 async + await

    方法一: 安装官方出的Microsoft.Bcl.Async包 最新发布日期为 2014/4/12,版本1.0.168 (不支持VS2010) 1.解决方案-右键-管理解决方案的NuGet程序包 2. ...

  2. [C#] .NET4.0中使用4.5中的 async/await 功能实现异

    好东西需要分享 原文出自:http://www.itnose.net/detail/6091186.html 在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framew ...

  3. 小心C# 5.0 中的await and async模式造成的死锁

    平时在使用C# 5.0中的await and async关键字的时候总是没注意,直到今天在调试一个ASP.NET项目时,发现在调用一个声明为async的方法后,程序老是莫名其妙的被卡住,就算声明为as ...

  4. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  5. .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  6. C#Framework4.0支持异步async/await语法

    由于用户使用的是XP系统,但是程序里异步都是通过async/await代码来实现的,然而async/await需要Framework4.5版本才可以,而XP系统最高只能支持到Framework4.0, ...

  7. Python的异步编程[0] -> 协程[0] -> 协程和 async / await

    协程 / Coroutine 目录 生产者消费者模型 从生成器到异步协程– async/await 协程是在一个线程执行过程中可以在一个子程序的预定或者随机位置中断,然后转而执行别的子程序,在适当的时 ...

  8. tornado5.0+async+await

    不使用数据库的情况下实现异步 使用gen.sleep()模拟阻塞 使用gen.sleep(time) 而不是time.sleep(),time.sleep()阻塞整个进程,看gen.sleep()源码 ...

  9. [转]小心C# 5.0 中的await and async模式造成的死锁

    原文链接 https://www.cnblogs.com/OpenCoder/p/4434574.html 内容 UI Example Consider the example below. A bu ...

随机推荐

  1. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

  2. C++ ADO 数据查询

    ADO 数据查询 关键点 上1条 下1条 第1条 最后1条 实现过程 // stdafx.h : include file for standard system include files, #im ...

  3. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

  4. JavaScript与Flash的通信

    当Flash置于HTML容器中时,经常会遇到AS与JS的通信问题,例如:JS能否调用AS中的变量.方法,AS能否调用JS中的变量.方法等等.答案是肯定的.随着技术的不断发展,解决方案也是多种多样的. ...

  5. OSGi 学习(二)

    上一篇说了很多虚的东西,现在说点别的. OSGi系统的独立环境下的项目结构以及启动脚本. 先说项目结构,基于equinox的OSGi容器的项目结构如下所示: bin中定义启动脚本,停止脚本之类的. c ...

  6. Python学习 之 异常

    1.python中处理异常的方式 #coding:utf8 filename=raw_input("请输入你要操作的文件") try: f=open(filename) print ...

  7. Redis 列表(List)

    Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个 ...

  8. Shell:sed流编辑器

    转载:http://blog.sina.com.cn/s/blog_ac9fdc0b0101lvdv.html sed和awk是永远地痛,学了又忘,主要是木有横向对比过,所以总把握不到精髓.它可以完美 ...

  9. C语言第四节数据类型、常量、变量

    数据 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文字数据.用迅 ...

  10. hashTable(哈希表)的基本用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...