【发布时间】:2018-12-17 11:40:54
【问题描述】:
我在节点中向 NewsAPI.org 发出 API 请求,如果我只是使用控制台日志,则会打印响应。现在我希望它在 var 'news' 中返回它并且它不返回它。此外,我在“updatedNews”中的函数返回仍然无法正常工作。
所以数据在控制台日志中返回,但没有保存在'var'中。
var news = 'news';
async function getNews() {
var responseNews = await axios
.get(
"https://newsapi.org/v2/top-headlines?country=us&apiKey=key"
)
.then( response => news = response.data.articles );
return responseNews;
}
var updatedNews = getNews()
console.log(news)
console.log(updatedNews)
【问题讨论】: