JS:带有 await 语句的异步函数导致未定义的承诺。我究竟做错了什么?简单例子

JS: Async function with await statement results in undefined promise. What am I doing wrong? Simple example

我是 JS 的新手,目前正沉浸在异步函数和承诺中。在 Python 和 R 方面有相当多的经验,这对我来说是全新的。我阅读了许多不同的网站、主题和解决方案,以在异步函数中 returning 一个值 - 但我无法让它工作。下面我将其归结为我编写的函数的简化,旨在从 google 获取有关位置的信息并 return 它。就那么简单。我听从了网上的建议,并尝试重写 Benjamin GruenbaumHow do I return the response from an asynchronous call?.

上的以下建议
async function foo(){
    var data = await fetch("/echo/json"); // notice the await
    // code here only executes _after_ the request is done
    return data.json(); // data is defined
}

请看下面我自己的代码。在我看来,我在做同样的事情,但它仍然记录为 Promise {<pending>}... 我做错了什么? data 应该是一个数组。即使我只替换 googleapi url 并使用 fetch().json(),它也会记录为 Promise {<pending>}

async function foo() {
  var data = await axios.get("https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam&key=API_KEY");
  return data;
}
      console.log(foo())

你能试试下面的代码来了解你得到的 "data" 中发生了什么。 这不是您问题的解决方案,但您会知道结果如何

console.log(JSON.stringify(foo()));

试试这种调用异步函数的方式

 async function foo() 
    {
      let response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=Amsterdam&key=API_KEY`);
      let data = await response.json()
      return data;
    }

    foo().then(data => console.log(data));