2 回答

TA贡献1806条经验 获得超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条经验 获得超11个赞
回调函数是异步的,try-catch 只能同步工作。您可以将 tryToCatch 设为异步函数并在 func() 调用之前添加 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
添加回答
举报