try/catch

在使用Async/Await前,我们可能这样写:

const main = (paramsA, paramsB, paramsC, done) => {
funcA(paramsA, (err, resA) => {
if (err) return done(err)
return funcB(paramsB, (err, resB) => {
if (err) return done(err)
funcC(paramsC, (err, resC) => {
if (err) return done(err)
// (╯°□°)╯︵ ┻━┻
return done(null, { resA, resB, resC })
})
})
})
}

采用了Async/Await后:

const main = async (paramsA, paramsB, paramsC) => {
const resA = await funcA(paramsA)
const resB = await funcB(paramsB)
const resC = await funcC(paramsC)
// \(T.T)/
return { resA, resB, resC }
}

然后你会发现,我们没有处理错误异常的情况,然后你可能会这么写:

const main = async (paramsA, paramsB, paramsC) => {
let resA
let resB
let resC
try {
resA = await funcA(paramsA)
} catch (error) {
throw error
}
try {
resB = await funcB(paramsB)
} catch (error) {
throw error
}
try {
resC = await funcC(paramsC)
} catch (error) {
throw error
}
// (o.o;)
return { resA, resB, resC }
}

可能你不需要单独处理每个错误,然后你就这么写:

const main = async (paramsA, paramsB, paramsC) =>
try {
const resA = await funcA(paramsA)
const resB = await funcB(paramsB)
const resC = await funcC(paramsC)
// (^.^')
return { resA, resB, resC }
} catch (error) {
throw error
}
}

但是,很快滴,你发现代码报了这样一个错误:(注意如果你用了Node.js,可能对这个错误不陌生)

(node: xxx) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: y): Error: some sort of error
(node: xxx) [DEP0018] 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.
// (-.-;)

然后你会想,我草,哪里的原因。然后你花了20多分钟,去找问题。问题很简单就是:

你抛出了一个异常,但是你没有捕捉它和处理它。

因为Async/Await函数返回的是一个Promise,所以我们想下怎么解决它:

const main = async (paramsA, paramsB, paramsC) => {
try {
const resA = await funcA(paramsA)
const resB = await funcB(paramsB)
const resC = await funcC(paramsC)
return { resA, resB, resC }
} catch (error) {
// sure it's thrown, but who catches it??
throw error
}
}
// somewhere else...
main()
.then(d => { // do things with the result })
.catch(e => { // handle that error! })

在.catch方法里面捕捉错误。但如果我真的想单独处理某个错误呢:

const main = async (paramsA, paramsB, paramsC) => {
const resA = await funcA(paramsA)
const resB = await funcB(paramsB).catch(e => { // things unique to this error })
const resC = await funcC(paramsC)
return { resA, resB, resC }
}
// ... all we need is this `.catch` to handle all of them.
main()
.then(d => { // do things with the result })
.catch(e => { // handle all other errors!! })

用Mocha, Sinon, and Chai进行测试

测试Async / Await函数非常简单,首先,你只要记住这三件事:

1 不要把异步函数和promise混一起

const thing = (params, done) => {
ApiCall(params, async (err, data) => {
if (err) return done(err)
const things = await OtherApiCall(data)
return done(null, things)
})
}

像上面这样,真的会崩溃的。

2 记住Async / Await返回了一个Promise

3 如果你在Mocha测试里面返回了promise,mocha测试会处理好它,你不用担心。

ok.开始:

我们有一个主文件main.js

// main.js
const main = async (paramsA, paramsB, paramsC) => {
const resA = await apiA.create(paramsA)
const resB = await apiB.delete(paramsB)
const resC = await apiC.update(paramsC)
return { resA, resB, resC }
}

然后我们的测试文件就这样写:

// test.js
const expect = require('chai').expect
const sinon = require('sinon')
const main = require('main.js')
const apiA = require('apiA')
const apiB = require('apiB')
const apiC = require('apiC')
describe('Main Function', () => {
let apiAstub
let apiBstub
let apiCstub
beforeEach(() => {
apiAstub = sinon.stub(apiA, 'create')
apiBstub = sinon.stub(apiB, 'delete')
apiCstub = sinon.stub(apiC, 'update')
})
afterEach(() => {
apiAstub.restore()
apiBstub.restore()
apiCstub.restore()
})
it('should handle errors if apiA.create() fails', () => {
apiAstub.throws('error for apiA.create()')
// 在这里处理一下。因为返回的是promise,catch一下就行
return main('a', 'b', 'c').catch((e) => {
//mocha会等promise返回或者在这里异常
expect(e).to.equal('error for apiA.create()')
})
})
it('should handle errors if apiB.delete() fails', () => {
apiAstub.returns('success a')
apiBstub.throws('error for apiB.delete()')
return main('a', 'b', 'c').catch((e) => {
expect(e).to.equal('error for apiB.create()')
})
})
it('should handle errors if apiC.update() fails', () => {
apiAstub.returns('success a')
apiBstub.returns('success b')
apiCstub.throws('error for apiC.delete()')
return main('a', 'b', 'c').catch((e) => {
expect(e).to.equal('error for apiC.create()')
})
})
it('should return the responses of all functions if all api calls succeed', () => {
apiAstub.returns('success a')
apiBstub.returns('success b')
apiCstub.throws('success c')
return main('a', 'b', 'c').then((res) => {
expect(res).to.deep.equal({
resA: 'success a',
resB: 'success b',
resC: 'success c',
})
})
})
})

不用第三方库,测试用例照写不误。

Async / Await和Node核心模块一起使用

你可能会想要这么写:

const fs = require('fs')
async function readThings () {
const file = await fs.readFile('./file.txt', 'utf8')
// file值不存在
return file
}

但实际上:readFile 并没有返回promise,这样写是有问题的

很幸运滴是Node.js8 util方法提供了一个method,promisify:

const fs = require('fs')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
async function readThings () {
const file = await readFile('./file.txt', 'utf8')
// 成功!
return file
}

Async / Await在AWS SDK中使用

const aws = require('aws-sdk')
async function getEc2Info () {
const ec2 = new aws.EC2()
const instances = await ec2.describeInstances()
// do things with instances
}

如果你像上面那样写,你会发现它不起作用。你会想用util.promisify,但其实它也不能用。

实际上,这样就行了:

const aws = require('aws-sdk')
async function getEc2Info () {
const ec2 = new aws.EC2()
const instances = await ec2.describeInstances().promise() // <--
// Actually do things with instances!
}

以上就是今天的内容,感谢阅读。

原文:https://start.jcolemorrison.com/5-tips-and-thoughts-on-async-await-functions/#testing

作者知乎/公众号:前端疯

(译文)学习ES6非常棒的特性——Async / Await函数的更多相关文章

  1. (译文)学习ES6非常棒的特性-深入研究var, let and const

    Var var firstVar; //firstVar被声明,它的默认值是undefined var secondVar = 2; //secondVar被声明,被赋值2 先看一个例子: var i ...

  2. (译文)学习ES6非常棒的特性-字符串常量基础

    字符串常量基础 在ES2015之前我们是这么拼接字符串的: var result = 10; var prefix = "the first double digit number I le ...

  3. 使用ES6新特性async await进行异步处理

    我们往往在项目中会遇到这样的业务需求,就是首先先进行一个ajax请求,然后再进行下一个ajax请求,而下一个请求需要使用上一个请求得到的数据,请求少了还好说,如果多了,就要一层一层的嵌套,就好像有点c ...

  4. 微信小程序捕获async/await函数异常实践

    背景 我们的小程序项目的构建是与web项目保持一致的,完全使用webpack的生态来构建,没有使用小程序自带的构建功能,那么就需要我们配置代码转换的babel插件如Promise.Proxy等:另外, ...

  5. ES6非常棒的特性-解构

    https://blog.csdn.net/maoxunxing/article/details/79772946

  6. .net 4.5 新特性 async await 一般处理程序实例

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Sys ...

  7. ES8之async/await学习随笔

    详细学习参考文档: 阮一峰老师的博客,覆盖知识点ES6/7/8/9,本篇学习笔记对阮老师的关于async/await文档中的知识点进行分点总结 在ES8中加入async/await新特性后,很明显带来 ...

  8. async/await,了解一下?

    上一篇博客我们在现实使用和面试角度讲解了Promise(原文可参考<面向面试题和实际使用谈promise>),但是Promise 的方式虽然解决了 callback hell,但是这种方式 ...

  9. ES7 Async/Await 陷阱

    什么是Async/Await ES6新增了Promise函数用于简化项目代码流程.然而在使用promise时,我们仍然要使用callback,并且并不知道程序要干什么,例如: function doS ...

随机推荐

  1. C#图解教程 第十一章 枚举

    枚举 枚举 设置底层类型和显式值隐式成员编号 位标志 Flags特性使用位标志的示例 关于枚举的补充 枚举 枚举 枚举是由程序员定义的类型与类或结构一样. 与结构一样,枚举是值类型,因此直接存储它们的 ...

  2. Vue安装依赖npm install时报错问题解决方法

    1.vue的安装依赖于node.js,要确保你的计算机上已安装过node.js.    可进入cmd编辑器,输入命令 node -v进行查看.出现版本信息即成功!没有则从浏览器上面下载安装即可,没有安 ...

  3. iOS逆向工程,(狗神)沙梓社大咖免费技术分享。

    序言 简介:本文针对于广大iOS开发者,作为一名开发者,仅仅专注于一门语言可能已经不适用现在的市场需求,曾经因高薪和需求量巨大,而火爆一时的移动端开发者(Android,ios),如今的路却是不再那么 ...

  4. redis3.0.7集群部署手册

    1.用root登录主机2.将redis-3.0.7.tar.gz传送到主机3.将rubygems-update-2.5.2.gem,redis-3.0.0.gem传送到主机4.解压redis-3.0. ...

  5. webpack开发环境配置和生产环境配置

    开发环境配置 在开发环境下,我们首先考虑的是方便开发,方便代码调试,不需要考虑代码合并和css样式分离这些. 这里主要说三个 :1.css模块化:2.模块热替换功能:3.source-map(代码映射 ...

  6. 原来你是这样的Websocket--抓包分析

    之前自己一个人负责完成了公司的消息推送服务,和移动端配合完成了扫码登录.订单消息推送.活动消息广播等功能.为了加深自己对Websocket协议的理解,自己通过进行抓包的方式学习了一番.现在分享出来,希 ...

  7. 毫秒级检测!你见过带GPU的树莓派吗?

    树莓派3B+英特尔神经计算棒进行高速目标检测 转载请注明作者梦里茶 代码: 训练数据预处理: https://gist.github.com/ahangchen/ae1b7562c1f93fdad1d ...

  8. redis笔记总结之redis数据类型及常用命令

    三.常用命令 3.1 字符串类型(string) 字符串类型是Redis中最基本的数据类型,一个字符串类型的键允许存储的数据的最大容量为512MB. 3.1.1 赋值与取值: SET key valu ...

  9. python 检测nginx状态,若无法访问发邮件通知

    应用场景:用来检测网站可用性,访问失败,则发邮件通知 #!/usr/bin/env python import urllib2,time,smtplib,string,logging from con ...

  10. tensorflow实现最基本的神经网络 + 对比GD、SGD、batch-GD的训练方法

    参考博客:https://zhuanlan.zhihu.com/p/27853521 该代码默认是梯度下降法,可自行从注释中选择其他训练方法 在异或问题上,由于训练的样本数较少,神经网络简单,训练结果 ...