如何优雅处理 async await 错误

5,674 阅读1分钟

 背景

发现团队里目前项目中在使用 async await 的时候,是通过 try catch 来进行错误处理的;而 try catch 这个东西本身的设计是处理未知错误的;并且大量的 try catch 使我们从promse的链式地狱转变成了 try catch 地狱

总结一下目前处理 async await 错误的两种方式

一、 promise.then.catch 的方式

如果await后面的异步操作出错,那么等同于async函数返回的 Promise 对象被reject

async function f() {
  await new Promise(function (resolve, reject) {
    throw new Error('出错了');
  });
}

f()
.then(v => console.log(v))
.catch(e => console.log(e))
// Error:出错了

或者

async function f() {
  await somethingThatReturnsAPromise()
  .catch(function (err) {
    console.log(err);
  });
}

二、try catch 的 方式

async function f() {
  try {
    await new Promise(function (resolve, reject) {
      throw new Error('出错了');
    });
  } catch(e) {
  }
  return await('hello world');
}

解决方案

在 await 处理 promise 返回的时候 增加一个数据的拦截机制

改造后

参考文章:await-to-js