1const mapLoop = async _ => {
2 console.log('Start')
3
4 const promises = fruitsToGet.map(async fruit => {
5 const numFruit = await getNumFruit(fruit)
6 return numFruit
7 })
8
9 const numFruits = await Promise.all(promises)
10 console.log(numFruits)
11
12 console.log('End')
13}
1var arr = [1, 2, 3, 4, 5];
2
3var results: number[] = await Promise.all(arr.map(async (item): Promise<number> => {
4 await callAsynchronousOperation(item);
5 return item + 1;
6}));
7
1const list = [1, 2, 3, 4, 5] //...an array filled with values
2
3const functionThatReturnsAPromise = item => { //a function that returns a promise
4 return Promise.resolve('ok')
5}
6
7const doSomethingAsync = async item => {
8 return functionThatReturnsAPromise(item)
9}
10
11const getData = async () => {
12 return Promise.all(list.map(item => doSomethingAsync(item)))
13}
14
15getData().then(data => {
16 console.log(data)
17})
18
1const arr = [1, 2, 3];
2
3const syncRes = arr.map((i) => {
4 return i + 1;
5});
6
7console.log(syncRes);
8// 2,3,4
9