Reputation: 131
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);
Upvotes: 0
Views: 99
Reputation: 17
It really makes no sense to return any data from callback.
You could just add a callback function to processData
and then call it in callback of readFile
.
Otherwise, you may use fs.readFileSync
, with this you can get file data synchronously.
Upvotes: 1