-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpromise.js
More file actions
79 lines (61 loc) · 1.92 KB
/
promise.js
File metadata and controls
79 lines (61 loc) · 1.92 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
const isAppleAvailableInMarket = true;
const isBreadAvailableInMarket = false;
const isAppleAvailable = () => {
return new Promise((resolve, reject) => {
if(isAppleAvailableInMarket) {
resolve('Yes!!! Apple is avalaible to the market !!!!')
} else {
reject('Opps !!! Apple are not avalaible to the market !!!!')
}
});
}
const isBreadAvailable = () => {
return new Promise((resolve, reject) => {
if(isBreadAvailableInMarket) {
resolve('Yes!!! Bread is avalaible to the market !!!!')
} else {
reject('Opps !!! Bread are not avalaible to the market !!!!')
}
});
}
const mainFunction = async() => {
// Using Promise Chain
// isAppleAvailable().then((res1) => {
// console.log(res1);
// return isBreadAvailable();
// }).then((res2) => {
// console.log(res2);
// }).catch((err) => {
// console.log(err);
// });
// Using Promise All
// Promise.all([isAppleAvailable(), isBreadAvailable()]).then((result) => {
// console.log(result)
// }).catch((err) => {
// console.log(err);
// })
// Using Async Await
try {
const appleStatus = await isAppleAvailable();
const breadStatus = await isBreadAvailable();
console.log("appleStatus:", appleStatus);
console.log("breadStatus", breadStatus);
} catch(err) {
console.log(err);
}
}
mainFunction();
// Promise you can do in home task
// const arr = [1, 4, 2, 3, 4, 'a', 'b'];
// eleminate char from arr
// Sort the array
// Distinct Element from array [ 1, 2, 3, 4]
// const promise1 = new Promise((resolve, reject) => {
// setTimeout(resolve, 200, "one");
// });
// const promise2 = new Promise((resolve, reject) => {
// setTimeout(resolve, 200, "two");
// });
// Promise.race([promise1, promise2]).then((value) => {
// console.log(value);
// });