An async/await compatible method with a forEach style signature
npm i foreach-async-await
There may have been times where you would like to use an async function inside a forEach loop.
[1, 2, 3, 4].forEach(async (num) => {
await someAsyncFunction(num);
});
As we know, the above example does not work. @async/foreach is a no frills, no dependencies module that makes iterating with async functions easier. @async/foreach takes advantage of Promise.all under the hood, so your invocations will run concurrently.
const asyncForEach = require('foreach-async-await');
const fs = require('fs');
(async () => {
const files = ['./path/to/file1.js', './path/to/file2.js', './path/to/file3.js'];
const filesContent = await asyncForEach(files, async (file, index, array) => {
console.log('The current index in the array:', index);
console.log('The entire array:', array);
await fs.readFile(file, 'utf8');
});
// Prints the contents of the files read
console.log(filesContent);
})();