I am trying to understand how asynchronous functions are used.
As an assigement I have to turn the following function:
var fs = require('fs');
function calculateByteSize(){
var totalBytes = 0,
i,
filenames,
stats;
filenames = fs.readdirSync(".");
for (i = 0; i < filenames.length; i++){
stats = fs.statSync("./" + filenames[i]);
totalBytes += stats.size;
}
console.log(totalBytes);
}
calculateByteSize();
Into its asynchronous version.
So far I got:
var fs = require('fs');
function calculateByteSize(){
var totalBytes = 0,
i,
stats;
fs.readdir(".", function(err, data){
for (i = 0; i < data.length; i++){
fs.stat("./" + data[i],function(err, stats){
totalBytes += stats.size;
});
}
});
console.log(totalBytes);
}
calculateByteSize();
Which works-- if I print the values of 'totalBytes' during the for cycle, I get the correct values.
However, the console.log statement prints the value '0', as it seems it get executed before the cycle.
Everything works fine if I use statSync as in the original function.
My question is: given the choice between a synchronous and asynchronous version of a function, which is best to use when nested into an asynchronous function?
Is there a way can I make sure my print statement is executed after the correct value is generated, without using a promise? (as in: Issue with nested asynchronous function not returning data in time)