-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromisesMethod.js
More file actions
113 lines (78 loc) · 2.9 KB
/
promisesMethod.js
File metadata and controls
113 lines (78 loc) · 2.9 KB
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
109
/*const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000, "resolved");
}); //will be resolved after 300ms
const promise2 = 93; //non-promise always marked as resolved
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "resolved2");
}); // will be resolved after 100ms
Promise.all([promise1, promise2, promise3])
.then((values) => {
console.log(values);
})
.catch((err) => {
console.log(err);
});*/
/*Promise.all Behavior:
-Promise.all will wait for promise1, promise2, and promise3 to resolve.
-The then handler of Promise.all will be called when all promises have resolved.*/
/*const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000,"one");
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 3000,"two");
});
const promise3 = new Promise((resolve, reject) => {
setTimeout(reject, 1000,"rejected");
});
Promise.all([promise1, promise2, promise3])
.then((values) => {
console.log(values);
})
.catch((error) => {
console.log(error);
});*/
/*
Promise.all Behavior:
-It takes an iterable of promises and returns a single promise that resolves when all of the promises in the iterable have resolved or rejects if any of the promises rejects.
-In this case, Promise.all will wait for all three promises to settle.
-Fail fast- If any promise in the iterable rejects, Promise.all will immediately reject with the reason of the first promise that rejects.*/
/*const SlowlyDone = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "Done slowly");
}); //resolves after 500ms
const QuicklyDone = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, "Done quickly");
}); //resolves after 100ms
const Rejection = new Promise((resolve, reject) => {
setTimeout(reject, 500, "Rejected"); //always rejected
});
Promise.any([SlowlyDone, QuicklyDone, Rejection])
.then((value) => {
console.log(value);
})
.catch((err) => {
console.log(err);
});*/
/*
Promise.any Behavior:
- Unlike Promise.all(), this method is used to return the first promise that fulfills.
- It is short-circuited right after a promise is completed, so as soon as a promise is fulfilled, it will not wait for other promises to complete.
- Note that, Promise.any() was supported in node.js 15.0.0.*/
const pro1 = new Promise((resolve, reject) => {
setTimeout(() => resolve("one"), 100);
});
const pro2 = new Promise((resolve, reject) => {
setTimeout(() => resolve("two"), 500);
});
const pro3 = new Promise((resolve, reject) => {
setTimeout(() => reject("rejected"), 100);
});
const pro4 = new Promise((resolve, reject) => {
setTimeout(() => resolve("four"), 400);
});
Promise.race([pro1, pro2, pro3, pro4])
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log(err);
});