无法理解异步获取调用

时间:2018-07-09 00:36:02

标签: javascript

我已经阅读了有关异步/等待的各种文章,但是仍然无法执行此简单操作。

const endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=Oakland&APPID=54d05c3dfefbf06b604ba3ffcaaabdcf'

const weather = [];

fetch(endpoint)
    .then(response => response.json())
    .then(data => weather.push(...data));

console.log(weather)
// returns blank arr []

我只想将我的数据放在一个数组中使用。我相信这是因为fetch在定义const weather之前正在运行吗?如果有人可以快速告诉我该放在哪里,那我将非常感激。

注意:我已经读过How do I return the response from an asynchronous call?,但仍然遇到问题。

0 个答案:

没有答案
相关问题