Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How to call result callback of async.map in JavaScript?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!