的签名d3.json已经改为从D3 V4到V5。它已从现已弃用的module d3-request 移至新的d3-fetchmodule。从 v5 开始,D3 使用Fetch API来支持旧的XMLHttpRequest,并反过来采用Promises来处理这些异步请求。
d3.json()no的第二个参数是处理请求的回调,而是一个可选RequestInit对象。d3.json()现在将返回一个您可以在其.then()方法中处理的 Promise 。
您的代码因此变为:
d3.json("/trip_animate/tripData.geojson")
  .then(function(data){
    // Code from your callback goes here...
  });
随着 Fetch API 的引入,调用的错误处理也发生了变化。v5 之前的版本使用传递给回调的第一个参数d3.json()来处理错误:
d3.json(url, function(error, data) { 
  if (error) throw error;
  // Normal handling beyond this point.
});
从 D3 v5 开始,d3.json()如果遇到错误,则返回的Promise将被拒绝。因此,可以应用处理这些拒绝的普通 JS 方法:
将拒绝处理程序作为第二个参数传递给.then(onFulfilled, onRejected)。
 
用于.catch(onRejected)向Promise添加拒绝处理程序。
 
应用第二个解决方案,您的代码就变成了
d3.json("/trip_animate/tripData.geojson")
  .then(function(data) {
    // Code from your callback goes here...
  })
  .catch(function(error) {
    // Do some error handling.
  });