何时需要对 async/await 进行异常捕获

何时需要对 async/await 进行异常捕获

try {
    // TypeError: Cannot read properties of undefined (reading 'data')
    // 就是当接口没有返回指定对象,而你又从 undefined 中解构的话,就会引发报错,就需要捕获,注意,这个请求拦截器应该是捕获不到的,因为这是解构出的错
    const { test: { data } } = await axios.get('http://localhost:9999/one')
    } catch (e) {
        console.error(e)
    }
posted @ 2024-05-19 11:15  朱在春  阅读(18)  评论(0)    收藏  举报