首页 > 解决方案 > D3 如何进一步传递 Promise.all 数据?

问题描述

你能看看吗?没有地图出现

https://codepen.io/DeanWinchester88/pen/BaZYewv

async function chart()  {
  const [data,geo]  = await Promise.all([
    d3.json(promises[0]),
    d3.json(promises[1])
  ])
  console.log("data",data)
  console.log("geo", geo)
}
chart()

let path = d3.geoPath()

function ready(error,data,geo){
  //topojson object
  let topoobject = topojson.feature(geo, geo.objects.counties);
  let counties  = topoobject.features;
  

标签: d3.jscharts

解决方案


您需要将数据传递给ready函数。现在在您的代码中,该ready函数永远不会被调用。你可以这样做:

Promise.all([
  d3.json(EDUCATION),
  d3.json(COUNTIES),
]).then(([data, geo]) => {
  ready(null, data, geo);
});

EDUCATION另外,网址有误。它应该是:

const EDUCATION = "https://cdn.freecodecamp.org/testable-projects-fcc/data/choropleth_map/for_user_education.json";
const COUNTIES = "https://cdn.freecodecamp.org/testable-projects-fcc/data/choropleth_map/counties.json";

最后,第一行ready拼写错误topojson


推荐阅读