我目前通过调用异步函数并将其与.then()链接来处理我的承诺。但我希望有一种更易读的方法。
我目前的可行方法是:
const apiCall = async() => { const response = await axios.get("URL"); return response; } apiCall().then(res => { console.log(res.data); });
我希望我的代码看起来像:
const apiCall = () => { const response = axios.get("URL); return response; } const fetchData = async() => { const response = await apiCall(); return response.data; } console.log(fetchData());
P粉7262346482024-04-06 09:27:18
如何
const apiCall = async() => { const { data } = await axios.get("URL"); return data; }