【发布时间】:2020-05-24 17:41:00
【问题描述】:
以下场景有什么区别:
场景 1 - axios 请求中的异步等待 - 不起作用
export const getAll = async () => {
const retrievedData = await axios.get('http://localhost:3001/anecdotes'); // should return - Array
return retrievedData.data;
};
const test = () => {
let anecdotes = getAll();
// do something
};
场景 2 - 辅助方法中的异步等待不起作用
export const getAll = () => {
const retrievedData = axios.get('http://localhost:3001/anecdotes'); // should return - Array
return retrievedData.data;
};
const test = async () => {
let anecdotes = await getAll();
// do something
};
场景 3 - 两个 WORKS 中的异步等待
export const getAll = async() => {
const retrievedData = await axios.get('http://localhost:3001/anecdotes'); // should return - Array
return retrievedData.data;
};
const test = async () => {
let anecdotes = await getAll();
// do something
};
以上场景有什么区别? 为什么只有第三种情况有效?
【问题讨论】:
标签: javascript reactjs promise async-await