Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:40:12+00:00 2026-06-16T00:40:12+00:00

I have the following code in my nodejs application: function someFileOperation(callback) { var files

  • 0

I have the following code in my nodejs application:

function someFileOperation(callback) {
    var files = ...;
    files.forEach(function (file) {
        doSomethingAsync(file, function (err, result) {

        });
    });
}

What is an elegant way to call the callback of someFileOperation() in case all doSomethingAsync() called their callback function and call it only once, when an error in doSomethingAsync() occurred?

For now I came up with something like this:

function someFileOperation(callback) {
    var files = ...;
    var noFiles = files.length;
    files.forEach(function (file) {
        doSomethingAsync(file, function (err, result) {
            if (err) {
                callback(err);
                callback = function () {}; // I don't want it to be called again
            } else if (--noFiles <= 0) {
                callback(null, true);
            }
        });
    });
}

But I think this is a lot of overhead for such a simple task. So I am looking for a much more elegant way or maybe a little framework for these kind of problems

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:40:13+00:00Added an answer on June 16, 2026 at 12:40 am

    Use async.map or async.foreach see here: https://github.com/caolan/async#map and https://github.com/caolan/async#forEach

    the async.map method takes an array of items and performs the same async call for each item in your array in parallel. If no errors are encountered, it will call a callback with a new array of results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code: var net = require('net'); var server = net.createServer(function (stream)
On my node.js server I have the following code: //Generate a token var token
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =
I have the following JS: http://monobin.com/__m1c171c4e and the following code: Code: var tpl =
I have the following code to encrypt/decrypt data in node.js, which is working: var
I have the following Node.js code: var express = require('express'); var app = express.createServer(express.logger());
I have the following code for accepting input from command line in nodejs for(i=0;
I need to sort nodes in xml. I have the following code which successfully
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.