小东博客

小东博客

千万不要因为走得太久,而忘记了我们为什么出发

立即进入⭐ 有关小东

复杂的技术,简单的讲解。

const awaitWrap = (promise) => {
  return promise
   .then(data => [null, data])
   .catch(err => [err, null])
 }
const [err, data] = await awaitWrap(fetchData())
function awaitWrap<T, U = any>(promise: Promise<T>):Promise<[U | null, T || null]>{
  return promise
    .then<[null, T]>((data: T) => [null, data])
    .catch<[U, null]>(err => [err, null])
}