1async function printFiles () {
2 const files = await getFilePaths();
3
4 await Promise.all(files.map(async (file) => {
5 const contents = await fs.readFile(file, 'utf8')
6 console.log(contents)
7 }));
8}
1 for (const file of files) {
2 const contents = await fs.readFile(file, 'utf8');
3 console.log(contents);
4 }
5
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
1public async Task RunAsync()
2{
3 var tasks = new List<Task>();
4
5 foreach (var x in new[] { 1, 2, 3 })
6 {
7 var task = DoSomethingAsync(x);
8 tasks.Add(task);
9 }
10
11 await Task.WhenAll();
12}
13
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.