优雅处理async&await的异常

·  阅读 105
const to = (promise) => {
  return promise
    .then(data => ([data, undefined]))
    .catch(error => Promise.resolve([undefined, error]));
}

 async function userProfile () {
  let [user, userErr] = await to(getUser());

  if(userErr) throw new Error('Could not fetch user details');
}
复制代码

----工作日常

分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改