I am not sure how can I get the data callback/return from a function inside a function.
I would like to pass the processed data from processData function as callback and return this bufferData to be use as input other function. At the moment, the bufferData can't be returned from the processData function as it calls from traverse function. The code is below:
function processData(obj, key, value){
var newobj=obj[key][0];
obj[key].push(newobj);
fs.readFile(__dirname + '/' + inFile, 'utf8', function(err, res) {
if (err) return console.log(err);
var data = JSON.parse(res);
data.key=obj;
bufferData=JSON.stringify(data);
**callback(bufferData);** (not sure how to code it to return this bufferData)
}); }
function traverse(obj, func) {
for (var key in obj) {
func(obj, key, obj[key]);
if (obj[key] !== null && typeof(obj[key])=="object") {
traverse(obj[key], func);
}
} }
traverse(data, processData);
callback(..)as he is not part of your code snippet. Can you please explain what you are trying to achieve. What you also should know is thers.readFileAsynchronously reads the entire contents of a file therefore yourtraversewill continue execution and not wait for thereadFileto complete.