title: NodeJS: Async return data from multiple TSV files in loop
body: <p>I'm trying to read a TSV data into an array and this <code>async</code> function loads the rows just fine, but the <code>rows.push(row)</code> is still delayed when I'm trying to get the rows outside the function.</p>
  <p>Is there a way I can get the row data for use outside this function? I've tried promises, but it seems like they still need to be within an async function.</p>
  <p>I'm looping through multiple files and reading the data for each, and I need to wait until all data is loaded to continue, so I don't think it makes sense to wrap everything else in a callback...</p>
  <pre class="lang-js prettyprint-override"><code>const csvToJson = require(&quot;csvtojson&quot;);
  var rows = [];
  const getRows = async (fileName) =&gt; {
      const csvData = await csvToJson({
        delimiter: &quot;\t&quot;,
        trim: true
      }).fromFile(fileName);
  
      csvData.forEach((row) =&gt; {
        rows.push(row);
      });
      return rows;
  };
  
  getRows(&quot;myfile.tsv&quot;);
  console.log(rows.length);  // prints 0
  setTimeout(function() {
    console.log(rows.length);
  }, 2000);                  // prints expected num of rows
  
  // do something else with rows
  doSomething(rows);
  </code></pre>
  <p>Alternatively, using callback, but I get a pending promise in return. I know I can resolve it with <code>then</code>, but that doesn't help me outside of <code>then</code>.</p>
  <pre><code>const getRows = async (callback, fileName) =&gt; {
      const csvData = await csvToJson({
        delimiter: &quot;\t&quot;,
        trim: true
      }).fromFile(fileName);
  
      let rows = [];
      csvData.forEach((row) =&gt; {
        rows.push(row);
      });
      return callback(rows);
  };
  
  // returns a pending promise
  var csvData = getRows(rows =&gt; {
    console.log(&quot;Rows in callback: &quot; + rows.length);     // expected length
    return rows;
  }, &quot;myfile.tsv&quot;);
  
  // do something else with data
  doSomething(csvData);
  </code></pre>