-2

I have this method 'allMuseums()' that calculates the variable 'visitatori_all_musei', now I want to return this variable in such a way that every time I call the method I can give the result.

function allMuseums() {
        var nomeFile = "dati_musei.csv";

        var mese_anno = [];

        d3.dsv(";", nomeFile, function(d) {
            return {
                Museo: d.Museo,
                Ingresso: d.Ingresso,
                Anno: d.Anno,
                Mese: d.Mese,
                Visitatori: d.Visitatori
            }
        }).then(function(data) {
            var visitatori_all_musei = filtraggio(data);
        });

    };

For example I want to do console.log(allMuseums()) and have the result. If instead I want to assign it to a variable? For example 'var data = allMuseums()'?

Danny
  • 165
  • 1
  • 12
  • you can't do `console.log(allMuseums())` until `allMuseums()` is async –  Sep 20 '18 at 16:03
  • @Danny check out [Up and Running with Asynchronous JavaScript](https://medium.com/@rcepeda1993/async-js-the-complete-guide-670b4cf906c6) – Rafael Sep 20 '18 at 16:08

2 Answers2

0

With ES6 async/await, you can use this:

async function allMuseums() {
    var nomeFile = "dati_musei.csv";
    var visitatori_all_musei;
    var mese_anno = [];

    await d3.dsv(";", nomeFile, function(d) {
        return {
            Museo: d.Museo,
            Ingresso: d.Ingresso,
            Anno: d.Anno,
            Mese: d.Mese,
            Visitatori: d.Visitatori
        }
    }).then(function(data) {
        visitatori_all_musei = filtraggio(data);
    });

    return visitatori_all_musei
};

With promises, you can use this:

function allMuseums() {
    var nomeFile = "dati_musei.csv";

    var mese_anno = [];

    return d3.dsv(";", nomeFile, function(d) {
        return {
            Museo: d.Museo,
            Ingresso: d.Ingresso,
            Anno: d.Anno,
            Mese: d.Mese,
            Visitatori: d.Visitatori
        }
    }).then(function(data) {
        return filtraggio(data);
    });
};

Which returns a promise, so you'll have to do allMuseums.then(console.log)

Rafael
  • 7,605
  • 13
  • 31
  • 46
Chris Stanley
  • 2,766
  • 2
  • 13
  • 18
  • If I code 'console.log(allMuseums())' returns undefined – Danny Sep 20 '18 at 16:04
  • @Danny The function returns a Promise, not undefined. In any case, you will have to unwrap the value. – Rafael Sep 20 '18 at 16:05
  • I was in the middle of a few things and completely wrote the wrong code, so I've updated my answer to better serve your needs. – Chris Stanley Sep 20 '18 at 16:08
  • @ChrisStanley inspect your async/await implementation – Rafael Sep 20 '18 at 16:09
  • If instead I want to assign it to a variable? For example 'var data = allMuseums()'? – Danny Sep 20 '18 at 16:13
  • I REALLY think you need to read up on promises, because you will not get every aspect of a promise from this series of questions. Your problem here is that you 100% have no understanding of how they work, and you need to learn about them. – Chris Stanley Sep 20 '18 at 17:11
0

Return the promise and the callback value:

function allMuseums() {
    var nomeFile = "dati_musei.csv";

    var mese_anno = [];

    //return here
    return d3.dsv(";", nomeFile, function (d) {
        return {
            Museo: d.Museo,
            Ingresso: d.Ingresso,
            Anno: d.Anno,
            Mese: d.Mese,
            Visitatori: d.Visitatori
        };
    }).then(data) {
        return filtraggio(data);//return here
    });
};

Using async/await:

async function allMuseums() {
    let nomeFile = "dati_musei.csv";
    let mese_anno = [];
    let data = await d3.dsv(";", nomeFile, function (d) {
        return {
            Museo: d.Museo,
            Ingresso: d.Ingresso,
            Anno: d.Anno,
            Mese: d.Mese,
            Visitatori: d.Visitatori
        };
    });
    return filtraggio(data);
};

Log the output:

allMuseums().then(console.log);

Assignment and whatever else:

allMuseums().then(data => {
   //do something here
   let myvar = data;
});
Rafael
  • 7,605
  • 13
  • 31
  • 46