lundi 20 janvier 2020

callback function returning undefined

I'm trying to return an object inside a callback function

In the following situation, the console.log() show the result as expected

var dVizModule = (function(){
    let dataset;

    function loadData(fileName) {
        dataset = d3.csv(fileName, (data) => {
            dataset = data;
            console.log(dataset);
        });
    };

    return {
        loadData: loadData
    }

})();

dVizModule.loadData("data/time_scale_data.csv")

but when I try to use return in callback function the story is different and it returns undefined

var dVizModule = (function(){
    let dataset;

    function loadData(fileName) {
        dataset = d3.csv(fileName, (data) => {
            dataset = data;
            return dataset;
        });
        // return dataset; or even here!
    };

    return {
        loadData: loadData
    }

})();

console.log(dVizModule.loadData("data/time_scale_data.csv"))

Aucun commentaire:

Enregistrer un commentaire