2

I have a javascript file "populationTest.js" which calls a second javascript file "populationReaderTest.js". The first one handles allocating the extracted data where it needs to go and the second filters through the filesystem to find the correct files and grabs the data. I can find the file, read the data and print it to the console but I do not know how return the data back to where it is called. Any tips on how I can achieve this?

Code snipit from "populationTest.js"

import {main as popread}  from "./populationtReaderTest.js"

...
...
    var data = popread("./js/actionsFile.txt");
    console.log(data);// prints undefined
    console.log(typeof(data));// prints undefined

Code snipit from "populationReaderTest.js"

var globe;
var main =  function(){

let path = "./js/actionsFile.txt" //path of the data to be extracted
d3.text(path,function(data){ 

    console.log(data) //successfully prints the extracted data to console
    globe = data;// doesn't work from here on
    return globe;

});

console.log(globe)
return globe;
};
export { main };

I am only using the global variables and console.log() for debugging and testing purposes final iteration will preferably not use global variables and will not log to console.

  • 2
    You have 2 big problems here: 1st, `d3.text` is asynchronous, so using global variables or whatever won't work. 2nd, `d3.text` doesn't return anything (actually it returns the object of the XHR request in your version, have a look at my [question/answer here](https://stackoverflow.com/q/47664292/5768908)), so the whole approach you're using won't work. – Gerardo Furtado Mar 05 '21 at 04:35
  • @GerardoFurtado Thank you, well back to square one. Any tips on how to achieve what I'm looking for, I've been working on this for a few days now. – Kdan Furtado Mar 05 '21 at 04:53
  • 1
    Sorry to say, but what is the object d3. As Gerardo says, being asynchronies means that it runs independently from the call, so the return value doesn't actually get passed back to the call in order to be assigned to anything. Your use of globe is fine, but you don't know when globe is given its value and when you try to print it. In order to get rid of global variables, in this case globe, means that you'll need to assign the function's value to a persistent variable or function property. Initially the variable or property will be undefined, but the value will be updated, eventually. ... –  Mar 05 '21 at 06:15
  • 1
    So use a setInterval function to monitor the result, and do something when the value is set to something other than undefined -- polling. Or better yet, instead of returning a value do whatever you need with the value within the function, show it in the console, put it in a screen element, etc. Finally, if the value is to be used in some other calculation, and you want to calculation's function to be separate from the function you show, then pass the function you show a callback function and have your function, the one you show in this question, call it. –  Mar 05 '21 at 06:20

0 Answers0