-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaining.js
64 lines (55 loc) · 1.3 KB
/
chaining.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
52
53
54
55
56
57
58
59
60
61
62
63
64
const promise = new Promise((resolve, reject) => {
resolve();
});
promise
.then(() => {
console.log('first');
})
.then(() => {
console.log('second');
})
.then(() => {
console.log('third');
})
.catch((error) => {
console.log('error ', error);
});
//EXAMPLE WITH setTimeout
const anotherPromise = new Promise((resolve, reject) => {
resolve();
});
anotherPromise
.then(() => {
console.log('first');
})
.then(() => {
setTimeout(() => {
console.log('second');
}, 2000);
})
.then(() => {
setTimeout(() => {
console.log('third');
}, 1000);
});
//ANOTHER EXAMPLE WITH setTimeout WITH ANOTHER PROMISE CALLED INSIDE
const thirdPromise = new Promise((resolve, reject) => {
resolve();
});
thirdPromise
.then(() => {
console.log('first');
})
.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('second');
resolve();
}, 2000);
});
})
.then(() => {
setTimeout(() => {
console.log('third');
}, 1000);
});