2 回答

TA貢獻1806條經(jīng)驗 獲得超8個贊
const tryToCatch = async (func, data ,dispatch) => { // Missing `async`
try {
await func()
console.log('mmmm')
} catch (error) {
console.log('Error: ', error)
console.log(data)
return handleError(error, data , dispatch)
}
}

TA貢獻1775條經(jīng)驗 獲得超11個贊
回調(diào)函數(shù)是異步的,try-catch 只能同步工作。您可以將 tryToCatch 設(shè)為異步函數(shù)并在 func() 調(diào)用之前添加 await。
const tryToCatch = async (func, data ,dispatch) => {
try {
await func()
console.log('mmmm')
} catch (error) {
console.log('Error: ', error)
console.log(data)
return handleError(error, data , dispatch)
}
}
參考:https ://javascript.info/try-catch
添加回答
舉報