title: Elegant way to call a callback function after other callbacks returned
body: <p>I have the following code in my nodejs application:</p>
  
  <pre><code>function someFileOperation(callback) {
      var files = ...;
      files.forEach(function (file) {
          doSomethingAsync(file, function (err, result) {
  
          });
      });
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>For now I came up with something like this:</p>
  
  <pre><code>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 &lt;= 0) {
                  callback(null, true);
              }
          });
      });
  }
  </code></pre>
  
  <p>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</p>