d3.js v5 - Promise.all replaced d3.queue

前端 未结 3 855
南旧
南旧 2020-11-30 11:15

I\'ve been using d3.js v4 for sometime now and I\'ve learned that Mike Bostock has replaced the d3.queue in the v5 release with the Promise native JavaScript object. I would

3条回答
  •  渐次进展
    2020-11-30 11:48

    You can simplify that code a lot: you don't net to use new Promise with d3.json, since d3.json will itself create the promise.

    So, you can just do:

    var files = ["data1.json", "data2.json", "data3.json"];
    var promises = [];
    
    files.forEach(function(url) {
        promises.push(d3.json(url))
    });
    
    Promise.all(promises).then(function(values) {
        console.log(values)
    });
    

    Or, if you're into the code golf, even shorter:

    var files = ["data1.json", "data2.json", "data3.json"];
    
    Promise.all(files.map(url => d3.json(url))).then(function(values) {
        console.log(values)
    });
    

    Since I cannot use JSON files in the S.O. snippet, check the console in this bl.ocks: https://bl.ocks.org/GerardoFurtado/f08993c9c729b0b3452ef1803ad9dcbf/c4b45c5acce6033085a667cbb7d34203d15de0f0

提交回复
热议问题