-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path051-Promise.js
108 lines (88 loc) · 3.71 KB
/
051-Promise.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
author : Jaydatt Patel
Promises : The Promise object represents the eventual completion or failure of an asynchronous operation and its resulting value.
constructor :
new Promise( (resolveFun, rejectFun) =>{});
A Promise is in one of these states:
- pending: initial state, neither fulfilled nor rejected.
- fulfilled: meaning that the operation was completed successfully.
- rejected: meaning that the operation failed.
The .then() method takes up to two arguments; the first argument is a callback function for the fulfilled case of the promise, and the second argument is a callback function for the rejected case. Each .then() returns a newly generated promise object, which can optionally be used for chaining;
Handling a rejected promise in each .then() has consequences further down the promise chain. Sometimes there is no choice, because an error must be handled immediately. In such cases we must throw an error of some type to maintain error state down the chain. On the other hand, in the absence of an immediate need, it is simpler to leave out error handling until a final .catch() statement. A .catch() is really just a .then() without a slot for a callback function for the case when the promise is fulfilled.
- Promise.all() : Fulfills when all of the promises fulfill; rejects when any of the promises rejects.
- Promise.allSettled() : Fulfills when all promises settle.
- Promise.any() : Fulfills when any of the promises fulfills; rejects when all of the promises reject.
- Promise.race() : Settles when any of the promises settles. In other words, fulfills when any of the promises fulfills; rejects when any of the promises rejects.
*/
//--------------------Sample-1--------------------
console.log(`----------Sample-1-----------`);
function getPromise(x) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`Promise initiated`);
if (!(x === 0)) {
resolve(x);
} else {
reject("Request rejected...");
}
}, 100);
});
}
let request1 = getPromise(0);
request1
.then((val) => {
console.log("val : ", val);
})
.catch((errVal) => {
console.log(errVal);
});
let request2 = getPromise(10);
request2
.then((val) => {
console.log("val : ", val);
})
.catch((errVal) => {
console.log(errVal);
});
//--------------------Sample-2--------------------
function getUserData(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`----------Sample-2-----------`);
if (userId === 123) {
resolve({ id: 123, name: "John Doe", age: 30 });
} else {
reject("User not found");
}
}, 150);
});
}
function displayUserData(userId) {
getUserData(userId)
.then((userData) => {
console.log("User data:", userData.name);
})
.catch((error) => {
console.error("Error: ", error);
});
}
displayUserData(123);
//--------------------Sample-3--------------------
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 250, "one");
});
const promise4 = new Promise((resolve, reject) => {
setTimeout(resolve, 200, "two");
});
Promise.race([promise3, promise4]).then((value) => {
console.log(`----------Sample-3-----------`);
console.log(value);
// Both resolve, but promise2 is faster
});
//--------------------Sample-4--------------------
const promise1 = new Promise((resolve) => setTimeout(() => resolve(1), 3000));
const promise2 = new Promise((resolve) => setTimeout(() => resolve(2), 1000));
Promise.all([promise1, promise2]).then((results) => {
console.log(`----------Sample-4-----------`);
console.log(results);
});