Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 like to check with you if this code that I have written is properly queuing (asynchronously) these URL's:

var makeRequest = function() {
    "use strict";

    var bli = [
        "http://stats.oecd.org/sdmx-json/data/BLI2013/all/all",
        "http://stats.oecd.org/sdmx-json/data/BLI2014/all/all",
        "http://stats.oecd.org/sdmx-json/data/BLI2015/all/all",
        "http://stats.oecd.org/sdmx-json/data/BLI2016/all/all",
        "http://stats.oecd.org/sdmx-json/data/BLI/all/all"
    ];

    var promises = [];

    bli.forEach(function(url) {
        promises.push(
            new Promise(function(resolve, reject) {
                d3
                    .json(url)
                    .then(function(response) {
                        resolve(response);
                    })
                    .catch(function(error) {
                        console.log("Error on: " + url + ". Error: " + error);
                        reject(error);
                    });
            })
        );
    });

    Promise.all(promises).then(function(values) {
        console.log(values);
    });
};

makeRequest();

The code seems to function properly, but, is this proper code or is there a better way (a best practice approach) for queuing with Promise.all and d3.js? Is the catch error properly implemented?

like image 665
Marco A. Ferra Avatar asked Mar 28 '18 12:03

Marco A. Ferra


People also ask

What does d3 queue () do?

d3. queue is used to run asynchronous tasks simultaneously and once the tasks are completed, perform operations on the results of the tasks.

Is d3 CSV asynchronous?

d3. csv is asynchronous by design to prevent pages from freezing up, so that can't be changed without changing the d3 library itself.

What is a promise in d3?

Data Visualization, Swizec Teller defines promises as follows: “a promise is an object that represents a value that may be available now, never, or sometime between those two extremes.” In the following code, I try to load in two CSV files: foo. csv and bar. csv using d3. csv.

What does d3 mean in JavaScript?

js (also known as D3, short for Data-Driven Documents) is a JavaScript library for producing dynamic, interactive data visualizations in web browsers. It makes use of Scalable Vector Graphics (SVG), HTML5, and Cascading Style Sheets (CSS) standards.


1 Answers

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

like image 67
Gerardo Furtado Avatar answered Oct 14 '22 10:10

Gerardo Furtado