首页 > 解决方案 > 将 promise 的内容保存到外部数组

问题描述

当我尝试在 之外的数组中捕获 Promise 的值时.then(),该数组被填充undefined(尽管console.log()记录了正确的值)。

  async function fetchCoordinates(pluscode) {
    let response = await fetch(url);
    return await response.json();
  }

  let places = []
  fetchCoordinates(pluscode)
  .then(geoinfo => {
    console.log(geoinfo)
    console.log(geoinfo.plus_code.geometry.location.lat)
    places.push(
      {position: new google.maps.LatLng(
        geoinfo.plus_code.geometry.location.lat, 
        geoinfo.plus_code.geometry.location.lng
      )}
    )
  })

标签: javascriptpromisefetch

解决方案


推荐阅读