We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
一般情况下async/await在错误处理方面,主要使用try/catch,像这样
async/await
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: ', data) } catch(err) { console.log('err: ', err) } })()
这么看,感觉倒是没什么问题,如果是这样呢?有多个异步操作,需要对每个异步返回的 error 错误状态进行不同的处理,以下是示例代码
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(() => { reject('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。
// ... 这里 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/await本质就是promise的语法糖,既然是promise那么就可以使用then函数了
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 () => { 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都写这么长,写着也不方便也不优雅,再优化一下
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 })()
The text was updated successfully, but these errors were encountered:
No branches or pull requests
一般情况下
async/await
在错误处理方面,主要使用try/catch
,像这样这么看,感觉倒是没什么问题,如果是这样呢?有多个异步操作,需要对每个异步返回的 error 错误状态进行不同的处理,以下是示例代码
这样写代码里充斥着
try/catch
,有代码洁癖的你能忍受的了吗?这时可能会想到只用一个try/catch
。如果是这样写只会增加编码的复杂度,而且要多写代码,这个时候就应该想想怎么优雅的解决,
async/await
本质就是promise
的语法糖,既然是promise
那么就可以使用then
函数了如果是这样写只会增加编码的复杂度,而且要多写代码,这个时候就应该想想怎么优雅的解决,async/await
本质就是promise
的语法糖,既然是promise
那么就可以使用then
函数了在上面写法中,如果 fetchData 返回 resolve 正确结果时,data 是我们要的结果,如果是 reject 了,发生错误了,那么 data 是错误结果,这显然是行不通的,再对其完善。
这样是不是好很多了呢,但是问题又来了,不能每个
await
都写这么长,写着也不方便也不优雅,再优化一下参考文献
The text was updated successfully, but these errors were encountered: