Parse JSON while asynchronously reading files
<p>I am trying to read a few JSON files and store their results into one array. I have:</p>

<pre><code>const files = ['file0.json', 'file1.json', 'file2.json', 'file3.json']
</code></pre>

<p>In order to read all of them and create a resulting array of the files' contents, I do this:</p>

<pre><code>import { readFile } from 'fs'
import async from 'async'

const files = ['file0.json', 'file1.json', 'file2.json', 'file3.json']

function read(file, callback) {
  readFile(file, 'utf8', callback)
}

async.map(files, read, (err, results) =&gt; {
  if (err) throw err

  // parse without needing to map over entire results array?
  results = results.map(file =&gt; JSON.parse(file))
  console.log('results', results)
})
</code></pre>

<p>This post helped me in getting there: <a href="https://stackoverflow.com/questions/9618142/asynchronously-reading-and-caching-multiple-files-in-nodejs">Asynchronously reading and caching multiple files in nodejs</a></p>

<p>What I'm wondering is how to call <code>JSON.parse()</code> in the intermediate step of reading the file instead of having to <code>map</code> over the resulting array. And I suppose a clarification on what exactly the <code>callback</code> parameter inside the read function is used for, if not just passed in for the sake of calling <code>readFile</code> properly.</p>