try/catch
,像這樣
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is me')
}, 1000)
})
}
(async () => {
try {
const data = await fetchData()
console.log('data is ->', data)
} catch(err) {
console.log('err is ->', err)
}
})()
複製代碼
這麼看,感受卻是沒什麼問題,若是是這樣呢?有多個異步操做,須要對每一個異步返回的 error 錯誤狀態進行不一樣的處理,如下是示例代碼typescript
const fetchDataA = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is A')
}, 1000)
})
}
const fetchDataB = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is B')
}, 1000)
})
}
const fetchDataC = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is C')
}, 1000)
})
}
(async () => {
try {
const dataA = await fetchDataA()
console.log('dataA is ->', dataA)
} catch(err) {
console.log('err is ->', err)
}
try {
const dataB = await fetchDataB()
console.log('dataB is ->', dataB)
} catch(err) {
console.log('err is ->', err)
}
try {
const dataC = await fetchDataC()
console.log('dataC is ->', dataC)
} catch(err) {
console.log('err is ->', err)
}
})()
複製代碼
這樣寫代碼裏充斥着 try/catch
,有代碼潔癖的你能忍受的了嗎?這時可能會想到只用一個 try/catch
。promise
// ... 這裏 fetch 函數省略
(async () => {
try {
const dataA = await fetchDataA()
console.log('dataA is ->', dataA)
const dataB = await fetchDataB()
console.log('dataB is ->', dataB)
const dataC = await fetchDataC()
console.log('dataC is ->', dataC)
} catch(err) {
console.log('err is ->', err)
// 難道要定義 err 類型,而後判斷嗎??
/** * if (err.type === 'dataA') { * console.log('dataA err is', err) * } * ...... * */
}
})()
複製代碼
若是是這樣寫只會增長編碼的複雜度,並且要多寫代碼,這個時候就應該想一想怎麼優雅的解決,async/await
本質就是 promise
的語法糖,既然是 promise
那麼就可使用 then
函數了異步
(async () => {
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is me')
}, 1000)
})
}
const data = await fetchData().then(data => data ).catch(err => err)
console.log(data)
})()
複製代碼
在上面寫法中,若是 fetchData 返回 resolve 正確結果時,data 是咱們要的結果,若是是 reject 了,發生錯誤了,那麼 data 是錯誤結果,這顯然是行不通的,再對其完善。async
(async () => {
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is me')
}, 1000)
})
}
const [err, data] = await fetchData().then(data => [null, data] ).catch(err => [err, null])
console.log('err', err)
console.log('data', data)
// err null
// data fetch data is me
})()
複製代碼
這樣是否是好不少了呢,可是問題又來了,不能每一個 await 都寫這麼長,寫着也不方便也不優雅,再優化一下函數
(async () => {
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('fetch data is me')
}, 1000)
})
}
// 抽離成公共方法
const awaitWrap = (promise) => {
return promise
.then(data => [null, data])
.catch(err => [err, null])
}
const [err, data] = await awaitWrap(fetchData())
console.log('err', err)
console.log('data', data)
// err null
// data fetch data is me
})()
複製代碼
將對 await
處理的方法抽離成公共的方法,在使用 await
調用 awaitWrap
這樣的方法是否是更優雅了呢。若是使用 typescript 實現大概是這個樣子fetch
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]) } 複製代碼
以上。優化