// Array.prototype.forEach is not designed for asynchronous code.
// Instead use await Promise.all to process all in parallel if the order doesn't matter.
await Promise.all(array.map(async (element) => {
await someFunction(element);
}))
[1, 2, 3].forEach(async (num) => { await waitFor(50); console.log(num);});console.log('Done');
Array.prototype.forEachAsync = async function (fn) {
for (let t of this) { await fn(t) }
}
Array.prototype.forEachAsyncParallel = async function (fn) {
await Promise.all(this.map(fn));
}
// 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.