-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.js
110 lines (92 loc) · 2.36 KB
/
day21.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
109
110
// Promise Basics Code -1
// How to create a new promise
const promiseOne = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("This Async task is Completed");
resolve("Done");
}, 1000);
});
// This .done is directly connected to resolve
promiseOne.then(function () {
console.log("The work is done");
});
// Promise 2
new Promise((resolve, reject) => {
setTimeout(() => {
console.log("This code will run after 5 seconds");
resolve();
}, 1000);
})
.then(function () {
console.log("The promise is resolved");
})
.catch(function () {
console.log("Some error Occured");
});
// Promoise - 3(data transfer from promise to then with the help of resolve)
const promisethree = new Promise(function (resolve, reject) {
setTimeout(() => {
resolve({ username: "Lokesh Singh", id: 25 });
}, 2000);
});
promisethree.then(function (user) {
console.log(user.username);
});
// promise - 4 (Handling of Errors)
const promisefour = new Promise(function (resolve, reject) {
setTimeout(() => {
let err = false;
if (!err) {
resolve({ username: "Lokesh Singh", id: 25 });
} else {
reject("error Occured");
}
}, 2000);
});
promisefour
.then(function (user) {
console.log(user.id);
})
.then(function (user) {
console.log(user.name);
})
.catch(function () {
console.log("Some error Occured");
})
.finally(() => console.log("The promise is resolved or Rejected"));
// Promise using Async and await
const PrmoiseFive = new Promise(function (resolve, reject) {
let err = true;
setTimeout(() => {
if (!err) {
resolve({ username: "sonali kumari", pass: "sonali@123" });
} else {
reject("ERROR in handling");
}
}, 2000);
});
async function consumePromiseFive() {
try {
let response = await PrmoiseFive;
console.log(response);
} catch (error) {
console.log(error);
}
}
consumePromiseFive();
// Fetch data from a Api
// async function Fetch_all_data(){
// let response = await fetch("https://jsonplaceholder.typicode.com/posts");
// const data = await response.json()
// console.log(data.userId);
// }
// Fetch_all_data()
// fetch Directly
fetch("https://jsonplaceholder.typicode.com/posts")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));