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) => {...
I have used a library called async. There a function called eachSeries
. It takes an array, an async function, and a callback. The function is called on each item for the array.
This question can open a rabbit hole of complexity. You will need to be careful that your async function doesn't make an asynchronous call. The library provides a callback that can be useful in this case.
function asyncFunction(val, callback) {
return (function() {
//do stuff
callback();
})();
}
The callback will initiate the a call on the next item in the array.