Say I have an array and I want to perform an async-function on each element of the array.
let a = [x1, x2, x3]
// I want to
await a.forEach(async (x) => {...
Like this?
for (let x of a) {
await fn(x);
}
Or if you really dislike creating a separate fn
:
for (let x of a) {
await (async v => {
...
})(x);
}
You could even add it to Array.prototype
:
Array.prototype.resolveSeries = async function(fn) {
for (let x of this) {
await fn(x);
}
}
// Usage:
await a.resolveSeries(fn);
// Or:
await a.resolveSeries(async x => {
...
});