【发布时间】:2021-05-08 08:02:37
【问题描述】:
当我使用 console.log(data) 时,我会记录我需要的信息,但如果在 getWeather() 中返回 data 的值,它只会返回一个待处理的 promise。我尝试了很多东西,但到目前为止都没有奏效。我将在下面留下我损坏的代码。
const axios = require('axios');
const getWeather = async () => {
try {
let response = await axios.get(
'http://api.openweathermap.org/data/2.5/forecast?id=524901&appid={apiKey}'
);
let data = response.data;
return data;
} catch (e) {
console.log(e);
}
};
async function returnAsync() {
const x = await getWeather();
return x;
}
console.log(getWeather()); // returns a pending promise
console.log('check ', returnAsync()); // also returns a pending promise
【问题讨论】:
-
async函数返回一个 Promise;这就是重点 -
实际上,他们应该返回一个Promise;
await表达式 always 会产生 Promise。 -
你需要将
console.loginsidereturnAsync函数,并记录x。
标签: javascript async-await axios