Javascript在函数中获取

时间:2017-03-09 03:27:30

标签: javascript function variables scope fetch-api

我试图从谷歌地图API获取一些JSON数据。当我调用此函数时,我想要"测试"变量变为45.但它仍然是0.
以下代码看起来有点愚蠢,因为我想简化一些事情。我有一个有效的API密钥请不要质疑。我误解了Javascript或fetch api还是什么?请帮忙..非常感谢!

### Subset df2, select the city of interest
df3 <- df2 %>%
  # In this example, assuming that City 2 and City 3 are of interest
  filter(City %in% c(2, 3))

### Plot the data
ggplot(data = df2, aes(x = Year, y = Value, group = factor(City))) +
  # Plot all city data here in gray lines
  geom_line(size = 1, color = "gray") +
  # Plot target city data with colors
  geom_line(data = df3, 
            aes(x = Year, y = Value, group = City, color = factor(City)),
            size = 2) 

1 个答案:

答案 0 :(得分:0)

fetch api返回一个Promise。这是一个异步过程,因此您需要在此处更新test的值:

then(function(data){ // here })