Here are some forEachAsync
prototypes. Note you'll need to await
them:
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));
}
Note while you may include this in your own code, you should not include this in libraries you distribute to others (to avoid polluting their globals).
To use:
await myArray. forEachAsyncParallel( async (item) => { await myAsyncFunction(item) })
Note you'll need a modern too like esrun
to use await at the top level in TypeScript.