title: Javascript Interview on promises
body: <p>This is a question I got asked in an interview. I know the array would just return zeros since this is asynchronous, but why does this happen, and how can you fix it so that the results array inserts the appropriate data?</p>
  
  <p><strong>Question:</strong> Suppose findData is a function that takes a query object and returns a promise for the result of the query. Suppose also that someRandomArrayOfQueries is an array of query objects. Explain what would be printed by the following code and why:</p>
  
  <pre><code>function runMultipleQueries(queries) {
   var results = [];
   queries.forEach(doQuery);
  
   return results;
  
   function doQuery(query) {
     findData(query)
     .then(results.push.bind(results));
   } 
  }
  
  function log(value) {
   console.log(value);
  }
  
  runMultipleQueries(someRandomArrayOfQueries).forEach(log);
  </code></pre>