优雅处理async&await的异常

159 阅读1分钟
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');
}

----工作日常