-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_poc.js
62 lines (53 loc) · 1.5 KB
/
promise_poc.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
var calculate = function (value) {
return new Promise(function (resolve, reject) {
console.log("in calculate : ", value);
setTimeout(function () {
if (value % 2 == 0) {
resolve(value + 1);
} else {
reject(value + 1);
}
}, 1000);
});
};
/**
Test Case 1 : send an Odd number in calculate,
Test Case 2 : send an Even number in calculate,
*/
function chainMethod() {
calculate(2)
.then(function (result) {
console.log("Then Here!!! ", result);
return calculate(result);
console.log("Never reach here Then-2 Here!!! ", result);
})
.catch(function (result) {
console.log("Catch Here!!! ", result);
return calculate(result + 1);
console.log(" Never reach here Catch-2 Here!!! ", result);
})
.then(function (result) {
console.log("Finally!!! then ", result);
})
.catch(function (result) {
console.log("Finally!!! catch ", result);
});
}
function safeMethod() {
calculate(1)
.then(function (result) {
console.log("Then Here!!! ", result);
return calculate(result);
}, function (result) {
console.log("Catch Here!!! ", result);
return calculate(result + 1);
})
.then(function (result) {
console.log("Finally!!! then ", result);
}, function (result) {
console.log("Finally!!! catch ", result);
});
}
// Comment one and try one by one both the methods, to understand the working of promise flow.
safeMethod();
// chainMethod();