Search code examples
javascriptd3.js

d3.csvParse inside d3.text => problem of sync


I am not expert in d3.js and i have to modify a program using it.

I have not found a good answer for my problem, i need to modify a file before to parse it: so i use this piece of code: (d3.js v4)

    d3.text(file, function (d) {
        //modify the content of file loaded
        d = d.replace(/ \(édito\)/g, "_edito")
            .replace(/ \(startup\)/g, "_startup");
        //parse after
        d3.csvParse(d, function (d) {
            console.log(d);
        });
    });
    console.log("next");

but the problem is d3.text, d3.csv are asynchronous and so console.log("next") is executed before the content of file line par line with console.log(d).

How i can wait the result of d3.txt ? before to continue...and without blocking the UI


Solution

  • in fact there is no problem, if i set all next codes in function

    d3.text(file, function (d) {
        //modify the content of file loaded
        d = d.replace(/ \(édito\)/g, "_edito")
            .replace(/ \(startup\)/g, "_startup");
        //parse after
        var result = d3.csvParse(d, function (d) {
            console.log(d);
            return d;
        });
        goToNext(result)
    });
    
    function goToNext(datas){
        console.log(datas);
    }