javascript map with async code example
Example 1: await inside map js
const mapLoop = async _ => {
console.log('Start')
const promises = fruitsToGet.map(async fruit => {
const numFruit = await getNumFruit(fruit)
return numFruit
})
const numFruits = await Promise.all(promises)
console.log(numFruits)
console.log('End')
}
Example 2: map with async
const list = [1, 2, 3, 4, 5]
const functionWithPromise = item => {
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 3: async map js
Arr = await Promise.all(arr)
Example 4: js is map async
let obj = {
one: 1,
two: 2,
three: 3
}
obj.map((element, index) => {
});