-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise_all.ts
53 lines (42 loc) · 1.3 KB
/
promise_all.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const getProductId = (category: { [key: string]: string }): Promise<string> => {
return new Promise((resolve, reject) => {
return setTimeout(() => resolve(category.id), 1000)
})
}
const capitalizeId = (id: string): Promise<string> => {
return new Promise((resolve, reject) => {
return setTimeout(() => resolve(id.toUpperCase()), 700)
})
}
const getProducts = () => {
return [
{ id: 'product1' },
{ id: 'product2' },
{ id: 'product3' },
{ id: 'product4' },
]
}
const capitalizeProductsIds = async () => {
const products = getProducts()
for (let product of products) {
const productId = await getProductId(product);
console.log(productId);
const capitalizedId = await capitalizeId(productId);
console.log(capitalizedId);
}
console.log(products);
}
const capitalizeProductsIdsParallel = async () => {
const products = getProducts()
await Promise.all(
products.map(async (product) => {
const productId = await getProductId(product);
console.log(productId);
const capitalizedId = await capitalizeId(productId)
console.log(capitalizedId);
})
)
console.log(products);
}
// capitalizeProductsIds()
capitalizeProductsIdsParallel()