签名
d3.json已经改为从D3V4到V5。它已从现已弃用的模块d3请求移至新的d3提取模块。从v5开始,D3使用Fetch
API来支持旧版本
XMLHttpRequest,并依次采用Promises来处理那些异步请求。
第二个参数
d3.json()不再是处理请求的回调,而是一个可选
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()如果遇到错误,将拒绝由返回的承诺。因此,可以应用处理这些拒绝的普通JS方法:
将拒绝处理程序作为第二个参数传递给
.then(onFulfilled, onRejected)
。用于
.catch(onRejected)
向承诺中添加拒绝处理程序。
应用第二个解决方案,您的代码就变成了
d3.json("/trip_animate/tripData.geojson") .then(function(data) { // Code from your callback goes here... }) .catch(function(error) { // Do some error handling. });


