xxxxxxxxxx
const arr: number[] = [1, 2, 3, 4, 5];
const result: number[] = await Promise.all(arr.map(async (elem): Promise<number> => {
await asyncFunction();
return elem + 10;
}));
// result === [11, 12, 13, 14, 15]
xxxxxxxxxx
const arr = [1, 2, 3];
const asyncRes = await Promise.all(arr.map(async (i) => {
await sleep(10);
return i + 1;
}));
console.log(asyncRes);
// 2,3,4
xxxxxxxxxx
const usernames = ['midudev', 'd4nidev', 'codingwithdani']
const posts = await Promise.all(
usernames.map(async (username) => {
return await fetchPostsFromTwitter(username)
})
)
console.log(posts)
/*
[
postsFromMidudev,
postsFromD4nidev,
postsFromCodingWithDani
]
*/
xxxxxxxxxx
const arrr = [1, 2, 3];
const asyncRes = await Promise.all(arrr.map(async (i) => {
await sleep(10);
return i + 1;
}));
console.log(asyncRes);
// 2,3,4
xxxxxxxxxx
const arr = [1, 2, 3];
const syncRes = arr.map((i) => {
return i + 1;
});
console.log(syncRes);
// 2,3,4