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的更多相关文章

  1. [Ramda] Handle Errors in Ramda Pipelines with tryCatch

    Handling your logic with composable functions makes your code declarative, leading to code that's ea ...

  2. [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 ...

  3. IOWebSocketChannel.connect handle errors

    https://github.com/dart-lang/web_socket_channel/issues/38 yes, my workaround is to create a WebSocke ...

  4. [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 ...

  5. 理解ASP.NET Core - 错误处理(Handle Errors)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或[点击此处查看全文目录](https://www.cnblogs.com/xiaoxiaotank/p/151852 ...

  6. [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 ...

  7. win32多线程-异步过程调用(asynchronous Procedure Calls, APCs)

    使用overlapped I/O并搭配event对象-----win32多线程-异步(asynchronous) I/O事例,会产生两个基础性问题. 第一个问题是,使用WaitForMultipleO ...

  8. win32多线程-异步(asynchronous) I/O

    I/O设备是个慢速设备,无论打印机.调制解调器,甚至硬盘,与CPU相比都奇慢无比,坐下来干等I/O的完成是一件不甚明智事情. 异步(asynchronous) I/O在win32多线程程序设计中被称为 ...

  9. Exceptions and Errors on iOS

    异常:程序缺陷导致:不可恢复:给开发者使用: 错误:资源受限导致:可恢复:提示给用户. https://blog.jayway.com/2010/10/13/exceptions-and-errors ...

随机推荐

  1. CSS控制显示超出部分,用省略号显示

    经常使用.可是常忘,我又不是写css的.所以记下来: 先设置一下限制的宽度, display:block; white-space:nowrap; overflow:hidden; text-over ...

  2. Chormium线程模型及应用指南

    核心概念 设计上遵循以下原则: 1 不要在UI线程做不论什么堵塞式的I/O操作,以及其他耗时的操作,通过消息传递把各种操作传给相应用途的线程去做. 2 不鼓舞线程加锁机制和线程安全对象. 对象仅仅存在 ...

  3. 重建一些被PHP7废弃的函数,

    <?php if(!function_exists('ereg')) { function ereg($pattern, $subject, &$matches = []) { retu ...

  4. softInputMode- 软件盘的设置

    今天遇到一个问题,就是软件盘弹出来以后,会把之前的布局界面整个的挤到屏幕的外面,而且按下返回建以后,这个软件盘占据的空间会留下一个黑色的背景.在网上查找了很多的方法,刚开始都是说,如下方法 <a ...

  5. String类型转Long类型需要注意的问题

    转自:https://blog.csdn.net/m819177045/article/details/52669785/

  6. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. Java 大数

    How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. NOIP2016 天天爱跑步(线段树/桶)

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 N个结点 ...

  9. 【Java学习】Font字体类的用法介绍

    一.Font类简介 Font类是用于设置图形用户界面上的字体样式的,包括字体类型(例如宋体.仿宋.Times New Roman等).字体风格(例如斜体字.加粗等).以及字号大小. 二.Font类的引 ...

  10. ListView-divider 分割线的设置

    1.去掉分割线 android:divider="@null" 2.设置分割线颜色跟宽度 android:divider="#19000000" android ...