本文地址 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 5278 Geometric Progression 高精度

    Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...

  2. Cocos2d-x学习笔记(10)(CCMenu菜单)

    1.CCMenu创建方式 CCMenu* menu = CCMenu::create(cocos2d::CCMenuItem* item,--)參数为CCMenuItem菜单项的对象可变參数列表 2. ...

  3. Linux - 打印文件夹全部文件 代码(C)

    列出文件夹全部文件 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 首先配置环境,參考:http://blog.csdn.net/caroline_wen ...

  4. android-betterpickers

    https://github.com/derekbrameyer/android-betterpickers

  5. CAShapeLayer和CAGradientLayer

    两个动画效果来了解一下CALayer的两个重要的subClass,CAGradientLayer和CAShapeLayer. 微视录制视频的时候那个进度效果和Spark相机类似,但是个人还是比较喜欢S ...

  6. 高级I/O之readv和writev函数

    readv和writev函数用于在一次函数调用中读.写多个非连续缓冲区.有时也将这两个函数称为散布读(scatter read)和聚集写(gather write). #include <sys ...

  7. Shell脚本调试工具set

    可以使用set命令的x选项,显示所有命令执行及变量值的变化过程等. 具体使用方法:首先使用set -x开启调试模式,最后使用命令set +x关闭调试模式. 一个简单示例演示如何使用set命令进行脚本调 ...

  8. 实例源码--Android时钟源码

      下载源码   技术要点: 1.Android自定义时钟控件 2.源码带有非常详细的中文注释 ...... 详细介绍: 1.Android自定义时钟控件 通过继承视图继承类View进行自定义控制,实 ...

  9. C++_快速排序

    void quick_sort(int s[],int l,int r) { if(l<r) { int i=l,j=r,x=s[l]; while(i<j) { while( i< ...

  10. CentOS6.5 一键部署运行环境shell脚本

     ################################################## #  CentOS6.5                                     ...