[ES7] Handle Errors in Asynchronous Functions
This lesson shows how regular control flow statements such as try
/catch
blocks can be used to properly handle errors in asynchronous functions. Oftentimes, the resulting code is easier to read than complex promise chains with .catch()
methods.
const fetch = require('node-fetch');
const BASE_URL = 'https://api.github.com/users'; class GithubUser {
async fetchGitHubUser(handle) {
const response = await fetch(`${BASE_URL}/${handle}`);
const body = await response.json(); if (response.status !== 200) {
throw Error(body.message);
} return body;
}
} (async () => {
const github = new GithubUser(); try {
const user = await github.fetchGitHubUser('zhentian-wan');
console.log(user);
} catch(err) {
console.error(err);
} })();
[ES7] Handle Errors in Asynchronous Functions的更多相关文章
- [Ramda] Handle Errors in Ramda Pipelines with tryCatch
Handling your logic with composable functions makes your code declarative, leading to code that's ea ...
- [Vue @Component] Handle Errors and Loading with Vue Async Components
Because async components are not bundled with your app, they need to be loaded when requested. This ...
- IOWebSocketChannel.connect handle errors
https://github.com/dart-lang/web_socket_channel/issues/38 yes, my workaround is to create a WebSocke ...
- [Javascript] Run asynchronous functions in sequence using reduce
This can be handy if you have a rate limit on API requests or if you need to pass the result of each ...
- 理解ASP.NET Core - 错误处理(Handle Errors)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或[点击此处查看全文目录](https://www.cnblogs.com/xiaoxiaotank/p/151852 ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- win32多线程-异步过程调用(asynchronous Procedure Calls, APCs)
使用overlapped I/O并搭配event对象-----win32多线程-异步(asynchronous) I/O事例,会产生两个基础性问题. 第一个问题是,使用WaitForMultipleO ...
- win32多线程-异步(asynchronous) I/O
I/O设备是个慢速设备,无论打印机.调制解调器,甚至硬盘,与CPU相比都奇慢无比,坐下来干等I/O的完成是一件不甚明智事情. 异步(asynchronous) I/O在win32多线程程序设计中被称为 ...
- Exceptions and Errors on iOS
异常:程序缺陷导致:不可恢复:给开发者使用: 错误:资源受限导致:可恢复:提示给用户. https://blog.jayway.com/2010/10/13/exceptions-and-errors ...
随机推荐
- C++ static 静态成员变量 和 静态成员函数
静态(static) 成员 变量 1• 静态成员变量的初始化须要在类外完毕. 2• 静态成员不属于详细的某个对象,而属于整个类: 3• 全部对象共享本类中的静态成员: 4• 静态成员最好直接通 ...
- python中lambda的另类使用
带if/else: ( lambda x, y: x if x < y else y )( 1, 2 ) 科里化: ( lambda x: ( lambda y: ( lambda z: x + ...
- 洛谷P1316 丢瓶盖
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- Manning.EJB.3.in.Action.2nd.Edition
Manning.EJB.3.in.Action.2nd.Edition http://files.cnblogs.com/files/rojas/EJB_3_in_Action_2nd_Edition ...
- javascript: with 表单验证
<html> <head> <script type="text/javascript"> function validate_required ...
- 右键菜单添加Total Commander
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Folder\shell\OpeninTC]@="使用TotalCmd打开(& ...
- 00090_字节输入流InputStream
1.字节输入流InputStream (1)通过InputStream可以实现把内存中的数据写出到文件: (2)把内存中的数据写出到文件InputStream此抽象类,是表示字节输入流的所有类的超类. ...
- Serializable中的serialVersionUID到底有啥用
最近在研究跨进程通信的问题,于是又再一次研究了,我们熟悉而又陌生的Serializable接口. 那么好,做过Java开发的朋友肯定对这个接口不陌生吧,Java中就是通过这个接口,来实现了序列化和反序 ...
- Redis源代码分析(八)--- t_hash哈希转换
在上次的zipmap分析完之后,事实上关于redis源码结构体部分的内容事实上已经所有结束了.由于以下还有几个和结构体相关的操作类,就页把他们归并到struct包下了.这类的文件有:t_hash.c, ...
- HTTP网络协议(二)
HTTP报文内的HTTP信息 HTTP协议交互的信息被称为HTTP报文,请求端的HTTP报文叫做请求报文,响应端的叫做响应报文. HTTP为了提升传输速率,其在传输数据时,按照数据原样进行压缩传 ...