nodejs await inside map
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')
}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)
})const someFunction = (myArray) => {
const promises = myArray.map(async (myValue) => {
return {
id: "my_id",
myValue: await service.getByValue(myValue)
}
});
return Promise.all(promises);
}
Also in JavaScript:
- read keyboard reactjs
- Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
- random function javascript
- make a fixed list in react native
- if variable is string javascript
- javascript replace doublequote with empty string
- jquery focus
- code intialization problem javascript
- react native charts
- javascript how to reverse a string
- jquery redirect to another webpage
- javascript grpc timestamp
- (!+[]+[]+![]).length 9
- smooth scrolll to id js
- object inside object javascript
- stopwatch with javascript
- change object key name javascript es6
- mv multiple directories
- form validation for email in js
- how to collect keys using lodash javascript
- how to get the last element of an array in javascript
- angular submit form programmatically
- flag svg javascript
- loop over json javascript