how to use async await with a for loop code example
Example 1: async foreach
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
asyncForEach([1, 2, 3], async (num) => {
await waitFor(50);
console.log(num);
})
Example 2: loop async javascript
const mapLoop = async _ => {
console.log('Start')
const numFruits = await fruitsToGet.map(async fruit => {
const numFruit = await getNumFruit(fruit)
return numFruit
})
console.log(numFruits)
console.log('End')
}