title: Optional callback not being called in the node.js async module's forEachOf method
body: <p>I'm using the async module's forEachOf method to print the end result after iterating through an object. Here is a shortened version of what I'm doing:</p>
  
  <pre><code>var async = require('async'),
      cheerio = require('cheerio'),
      request = require('request');
  
  var returnArray = [];
  
  async.forEachOf(myObj, function (value, key, callback) {
      var anotherObj = {};
  
      anotherObj.type = "val1";
  
      request(someurl, function(err, res, body) {
          if (err) {
              return callback(err);
          }
  
          var $ = cheerio.load(body);
  
          anotherObj.name = "val2";
  
          var miniObj = {};
          $('#some-element', "#an-id").each(function(i, value) {
              var val = value.value;
              miniObj[size] = val;
          });
  
          anotherObj.miniObj = miniObj;
  
          returnArray.push(anotherObj);
  
          return callback();
      });
  }, function (err) {
      if (err) {
          console.error(err.message);
      }
  
      console.log(returnArray);
  });
  </code></pre>
  
  <p>However, when I run the program, nothing (namely, 'returnArray') gets printed to the console like it should be.</p>
  
  <p>For reference, I have looked at these other, similar posts:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9470856/using-async-module-to-fire-a-callback-once-all-files-are-read">Using async module to fire a callback once all files are read</a> (seems outdated, is using the wrong method, and doesn't address the issue)</li>
  <li><a href="https://stackoverflow.com/questions/16375983/last-callback-not-being-called-using-async">Last callback not being called using async</a> (doesn't address the issue)</li>
  </ul>
  
  <p>I'm not sure what I'm doing wrong here. Could anyone please point out what I'm doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: So I think I finally figured out what I was doing wrong. In a different example I provided earlier <a href="http://code.runnable.com/VZELB92Qa1Y7JQuq/async-foreachof-node-js-not-working-2-for-callback" rel="nofollow noreferrer">HERE, using Runnable</a> I forgot to add a 'return callback()' statement. Then it worked. The only difference between that and this example being that my 'return callback()' statement in the above-provided example is itself called within another asynchronous method. I think in order to fix my problem I will somehow have to make sure (probably using some control flow function in async's library) to call 'return callback()' at the correct 'scope/level' after the second asynchronous method has finished. I think I will attribute this, my mistake, to the lack of documentation on proper usage of the 'return callback()' statement in the async docs. I will update this post with a solution once I figure it out (and catch up on some sleep)!</p>