nodejs7.0 试用 async await
本文地址 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)