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 arr = [1, 2, 3];
2
3const asyncRes = await Promise.all(arr.map(async (i) => {
4 await sleep(10);
5 return i + 1;
6}));
7
8console.log(asyncRes);
9// 2,3,4
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
1let obj = {
2 one: 1,
3 two: 2,
4 three: 3
5}
6
7obj.map((element, index) => {
8 // do something
9 // return result
10});
11
12/*
13 map() is a higher order function (a function that takes another
14 function as a parameter) and as such is synchronous
15*/