I want to understand a mechanism of callbacks, so i try to compute a summary size for files in one directory. Here is my code:
import path from 'path';
import fs from 'fs';
import async from 'async';
export const getDirectorySize = (directory) => {
fs.readdir(directory, (error, files) => {
if (error) throw error;
const counter = (el, callback) => {
fs.stat(path.join(directory, el), (error2, result) => {
if (error2) throw error2;
if (result.isFile()) callback(null, result.size);
});
}
async.map(files, counter, (error3, results) => {
if (error3) throw error3;
console.log(results.reduce((prev, current) => prev + current, 0));
});
});
}
getDirectorySize('/usr/src/app');
So when i run code above, the third argument (callback) of async.map doesn't call. But function "counter" is called (There were no errors)! So how to see the results of async.map?