-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasynchronous-pro.js
51 lines (39 loc) · 1.22 KB
/
asynchronous-pro.js
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
const delay = (time) => {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
};
let time = Date.now();
const log = () => console.log(`Elapsed: ${(Date.now() - time) / 1000}s`);
const getVeggie = async (name) => {
// Stop the Execution thread for 1s.
await delay(1000);
const veggies = {
mango: "🥭",
apple: "🍎",
banana: "🍌",
avocado: "🥑",
};
console.log("returned", name);
return veggies[name];
};
const getSalad = async () => {
const mango = await getVeggie("mango");
const apple = await getVeggie("apple");
const avocado = await getVeggie("avocado");
const banana = await getVeggie("banana");
return [mango, apple, avocado, banana];
};
// getSalad().then((response) => console.log(response));
const getSaladFaster = async () => {
const mango = getVeggie("mango");
const apple = getVeggie("apple");
const avocado = getVeggie("avocado");
const banana = getVeggie("banana");
return Promise.all([mango, apple, avocado, banana]);
};
// getSaladFaster().then((response) => console.log(response));
// Note the time difference in execution between these two functions
// Run them one by one
getSalad().then(log); // ~4sec
getSaladFaster().then(log); // ~1sec