javascript foreach await doesn work code example
Example 1: foreach await js
async function printFiles () {
const files = await getFilePaths();
for (const file of files) {
const contents = await fs.readFile(file, 'utf8');
console.log(contents);
}
}
Example 2: js await in foreach
// Javascript will proceed to call the code that comes AFTER the forEach loop,
// and then execute the code within the loop. This is because forEach is not
// async-aware. YOU CANNOT USE AWAIT IN FOREACH. Use a regular for loop instead.