how to use async and await with array.map() code example
Example 1: map with async
const list = [1, 2, 3, 4, 5] //...an array filled with values
const functionWithPromise = item => { //a function that returns a promise
return Promise.resolve('ok')
}
const anAsyncFunction = async item => {
return functionWithPromise(item)
}
const getData = async () => {
return Promise.all(list.map(item => anAsyncFunction(item)))
}
getData().then(data => {
console.log(data)
})
Example 2: async map js
Arr = await Promise.all(arr)