asyncronus foreach in typesrypt 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: typescript foreach async await
export async function asyncForEach<T>(array: Array<T>, callback: (item: T, index: number) => void) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index);
}
}
await asyncForEach(receipts, async (eachItem) => {
await ...
})
Example 3: C# foreach loop async but wait at end
public async Task RunAsync()
{
var tasks = new List<Task>();
foreach (var x in new[] { 1, 2, 3 })
{
var task = DoSomethingAsync(x);
tasks.Add(task);
}
await Task.WhenAll();
}