Using async/await with for loop in nodejs

Looping using for or foreach loop to read in sequence in nodejs

async function printFiles () {
  const files = await getFilePaths() 

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  }
}

Looping using map function to read in parallel in nodejs

async function printFiles () {
const files = await getFilePaths();

await Promise.all(files.map(async (file) => {
const contents = await fs.readFile(file, 'utf8')
console.log(contents)
}));
}

Leave a Reply.. code can be added in <code> </code> tags