title: How to correctly perform async I/O in map iterator in node.js?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/9518414/cannot-use-map-function-within-async-module">Cannot use “map” function within async module</a>  </p>
  </blockquote>
  
  
  
  <p>I've got a problem like this:</p>
  
  <pre><code>    var paths = ['path1', 'path2', 'path3', ...]; //Some arbitrary array of paths
  
      var results; //I need to collect an array of results collected from these paths
  
  results = paths.map(function(path){
    var tempResult;
  
    GetData(path, function(data){ //Third-party async I/O function which reads data from path
      tempResult = data;
    });
  
    return tempResult;
  });
  
  console.log(results); //returns something like [nothing, nothing, nothing, ...]
  </code></pre>
  
  <p>I can imagine why it happens (<code>return tempResult</code> fires before the async function returned any data - it's slow after all), but can't quite see how to make it right.</p>
  
  <p>My guess is <a href="https://github.com/caolan/async#map" rel="nofollow noreferrer">async.map</a> might help, but I fail to see how right away.</p>
  
  <p>Maybe someone more experienced in asynchronous programming might explain the way?</p>