使用async和await获取axios的数据注意事项

使用async和await获取axios的数据的注意事项

确定正确使用

async function getInfo() {
  const res = await axios.get('http://example.com')
  return res.data
}

上述代码等同于

async function getInfo() {
  const result = (await axios.get('http://example.com')).data
  return result
}

注意括号包裹,如果没有括号则返回为 undefined

posted @ 2023-11-02 10:56  浮生阁阁主  阅读(114)  评论(0)    收藏  举报