原文连接:Better error handling with async/await,by Sobio Darlingtonjavascript
本篇文章介绍在使用 async
/await
语法时,一种更好的处理错误的方式。在此以前,你们也须要先了解下 Promise 的工做原理。java
回调地狱(callback Hell),也称为“末日金字塔(Pyramid of Doom)”,是在开发者代码中看到的一种反模式(anti-pattern),这种异步编程方式并不明智。- Colin Tohnode
因为回调函数的嵌套,回调地狱 会使你的代码向右排布而不是垂直向下排版。npm
// Code that reads from left to right
// instead of top to bottom
let user;
let friendsOfUser;
getUser(userId, function(data) {
user = data;
getFriendsOfUser(userId, function(friends) {
friendsOfUser = friends;
getUsersPosts(userId, function(posts) {
showUserProfilePage(user, friendsOfUser, posts, function() {
// Do something here
});
});
});
});
复制代码
Promise 是 ES2015(即俗称的 ES6)引入的一个语言特性,用来更好的处理异步操做,避免回调地狱的出现。promise
下例中使用 Promise 的 .then
链来解决回调地狱问题。异步
// A solution with promises
let user;
let friendsOfUser;
getUser().then(data => {
user = data;
return getFriendsOfUser(userId);
}).then(friends => {
friendsOfUser = friends;
return getUsersPosts(userId);
}).then(posts => {
showUserProfilePage(user, friendsOfUser, posts);
}).catch(e => console.log(e));
复制代码
Promise 的处理方式更加干净和可读。async
async
/await
是一种特殊的语法,能够用更简洁的方式处理 Promise。异步编程
在 funtion
前加 async
关键字就能将函数转换成 Promise。函数
全部的 async 函数的返回值都是 Promise。
// Arithmetic addition function
async function add(a, b) {
return a + b;
}
// Usage:
add(1, 3).then(result => console.log(result));
// Prints: 4
复制代码
使用 async
/await
,可让“用户资料案例 2”看起来更棒。
async function userProfile() {
let user = await getUser();
let friendsOfUser = await getFriendsOfUser(userId);
let posts = await getUsersPosts(userId);
showUserProfilePage(user, friendsOfUser, posts);
}
复制代码
在“用户资料案例 3”中,若是有一个 Promise reject 了,就会抛出 Unhandled promise rejection
异常。
在此以前写的代码都没有考虑 Promise reject 的状况。未处理的 reject Promise 过去会以静默的方式失败,这可能会使调试成为噩梦。
不过如今,Promise reject 时会抛出一个错误了。
VM664:1 Uncaught (in promise) Error
(node:4796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (r ejection id: 1): Error: spawn cmd ENOENT
[1] (node:4796) 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
。
No promise should be left uncaught(每一个 Promise 都要使用
.catch
处理). - Javascript
注意,“用户资料案例 2”中的 .catch
方法。若是没有写 .catch
块的话,JavaScript 会在 Promise reject 的时候抛出 Unhandled promise rejection
错误。
处理“用户资料案例 3”中的问题比较容易。只要使用 try...catch
块包装下 await
语句就能避免 Unhandled promise rejection
错误了。
async function userProfile() {
try {
let user = await getUser();
let friendsOfUser = await getFriendsOfUser(userId);
let posts = await getUsersPosts(userId);
showUserProfilePage(user, friendsOfUser, posts);
} catch(e) {
console.log(e);
}
}
复制代码
问题解决了!
我怎么知道报错是来自哪个异步请求的呢?
能够在异步请求上使用 .catch
方法来处理错误。
let user = await getUser().catch(e => console.log('Error: ', e.message));
let friendsOfUser = await getFriendsOfUser(userId).catch(e => console.log('Error: ', e.message));
let posts = await getUsersPosts(userId).catch(e => console.log('Error: ', e.message));
showUserProfilePage(user, friendsOfUser, posts);
复制代码
上面的解决方案将处理来自请求的单个错误,可是会混合使用多种模式。应该有一种更干净的方法来使用 async
/await
而不使用 .catch
方法(嗯,若是你不介意的话,能够这样作)。
/** * @description ### Returns Go / Lua like responses(data, err) * when used with await * * - Example response [ data, undefined ] * - Example response [ undefined, Error ] * * * When used with Promise.all([req1, req2, req3]) * - Example response [ [data1, data2, data3], undefined ] * - Example response [ undefined, Error ] * * * When used with Promise.race([req1, req2, req3]) * - Example response [ data, undefined ] * - Example response [ undefined, Error ] * * @param {Promise} promise * @returns {Promise} [ data, undefined ] * @returns {Promise} [ undefined, Error ] */
const handle = (promise) => {
return promise
.then(data => ([data, undefined]))
.catch(error => Promise.resolve([undefined, error]));
}
async function userProfile() {
let [user, userErr] = await handle(getUser());
if(userErr) throw new Error('Could not fetch user details');
let [friendsOfUser, friendErr] = await handle(
getFriendsOfUser(userId)
);
if(friendErr) throw new Error('Could not fetch user\'s friends');
let [posts, postErr] = await handle(getUsersPosts(userId));
if(postErr) throw new Error('Could not fetch user\'s posts');
showUserProfilePage(user, friendsOfUser, posts);
}
复制代码
这里使用了一个工具函数 handle
,如此就能够避免 Unhandled promise rejection
报错,还能细粒度的处理错误。
handle
函数接受一个 Promise 对象做为参数,并老是 resolve 它,以 [data|undefined, Error|undefined]
的形式返回结果。
handle
函数返回 [data, undefined]
;handle
函数返回 [undefined, Error]
。async
/await
中的错误,Jesse Wardenasync
/await
的语法很简洁,但你仍是要处理异步函数里的抛出的错误。
除非你实现了自定义错误类(custom error classes),不然很难处理 Promise.then
链中的 .catch
错误处理。
使用 handle
工具函数,咱们能够避免 Unhandled promise rejection
报错,还能细粒度的处理错误。
(正文完)
广告时间(长期有效)
我有一位好朋友开了一间猫舍,在此帮她宣传一下。如今猫舍里养的都是布偶猫。若是你也是个爱猫人士而且有须要的话,不妨扫一扫她的【闲鱼】二维码。不买也没关系,看看也行。
(完)