forked from DouglasHdezT/JavaScript_NodeCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.js
More file actions
51 lines (36 loc) · 915 Bytes
/
Example2.js
File metadata and controls
51 lines (36 loc) · 915 Bytes
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
/**
* Await call
*/
const sqrAfterYSeconds = (x, y) => {
return new Promise(resolve => {
setTimeout(() => {
let result = x * x;
resolve(result);
}, y);
});
}
async function delayedPow1(x) {
let time = 0;
const interval = setInterval(() => console.log(`1: ${++time}`)
, 1000);
const a = await sqrAfterYSeconds(20, 2000);
const b = await sqrAfterYSeconds(30, 5000);
clearInterval(interval);
return x + a + b;
}
async function delayedPow2(x) {
let time = 0;
const interval = setInterval(() => console.log(`2: ${++time}`)
, 1000);
const a = sqrAfterYSeconds(20, 2000);
const b = sqrAfterYSeconds(30, 5000);
let result = x + await a + await b;
clearInterval(interval);
return result;
}
delayedPow2(10).then(v => {
console.log(v);
});
/* delayedPow2(10).then(v => {
console.log(v);
}); */