1 for (const file of files) {
2 const contents = await fs.readFile(file, 'utf8');
3 console.log(contents);
4 }
5
1[1, 2, 3].forEach(async (num) => { await waitFor(50); console.log(num);});console.log('Done');
1Array.prototype.forEachAsync = async function (fn) {
2 for (let t of this) { await fn(t) }
3}
4
5Array.prototype.forEachAsyncParallel = async function (fn) {
6 await Promise.all(this.map(fn));
7}
8
1async function printFiles () {
2 const files = await getFilePaths();
3
4 for (const file of files) {
5 const contents = await fs.readFile(file, 'utf8');
6 console.log(contents);
7 }
8}
9
1// Javascript will proceed to call the code that comes AFTER the forEach loop,
2// and then execute the code within the loop. This is because forEach is not
3// async-aware. YOU CANNOT USE AWAIT IN FOREACH. Use a regular for loop instead.