Skip to content

Commit ce75447

Browse files
12-Advanced JavaScript added
1 parent 817abe0 commit ce75447

File tree

5 files changed

+312
-0
lines changed

5 files changed

+312
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# API Request and V8 Engine 🚀🔥
2+
3+
## XMLHttpRequest : readyState property ✅
4+
5+
### _The XMLHttpRequest.readyState property returns the state an XMLHttpRequest clients is in. An XHR client exists in one of the following states:_
6+
7+
<table>
8+
<tr>
9+
<th>Value</th>
10+
<th>State</th>
11+
<th>Description</th>
12+
</tr>
13+
<tr>
14+
<td>0</td>
15+
<td>UNSENT</td>
16+
<td>Client has been created. open() not called yet.</td>
17+
</tr>
18+
<tr>
19+
<td>1</td>
20+
<td>OPENED</td>
21+
<td>open() has been called.</td>
22+
</tr>
23+
<tr>
24+
<td>2</td>
25+
<td>HEADERS_RECEIVED</td>
26+
<td>send() has been called, and headers and status are available.</td>
27+
</tr>
28+
<tr>
29+
<td>3</td>
30+
<td>LOADING</td>
31+
<td>Downloading; responseText holds partial data.</td>
32+
</tr>
33+
<tr>
34+
<td>4</td>
35+
<td>DONE</td>
36+
<td>The operation is complete.</td>
37+
</tr>
38+
</table>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>API Request</title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<div id="main">
11+
<table>
12+
<tr>
13+
<th>Value</th>
14+
<th>State</th>
15+
<th>Description</th>
16+
</tr>
17+
<tr>
18+
<td>0</td>
19+
<td>UNSENT</td>
20+
<td>Client has been created. open() not called yet.</td>
21+
</tr>
22+
<tr>
23+
<td>1</td>
24+
<td>OPENED</td>
25+
<td>open() has been called.</td>
26+
</tr>
27+
<tr>
28+
<td>2</td>
29+
<td>HEADERS_RECEIVED</td>
30+
<td>send() has been called, and headers and status are available.</td>
31+
</tr>
32+
<tr>
33+
<td>3</td>
34+
<td>LOADING</td>
35+
<td>Downloading; responseText holds partial data.</td>
36+
</tr>
37+
<tr>
38+
<td>4</td>
39+
<td>DONE</td>
40+
<td>The operation is complete.</td>
41+
</tr>
42+
</table>
43+
</div>
44+
</body>
45+
<script>
46+
// AJAX Scripting
47+
const requestUrl = "https://api.github.com/users/ChinmayKaitade";
48+
const xhr = new XMLHttpRequest();
49+
xhr.open("GET", requestUrl);
50+
xhr.onreadystatechange = function () {
51+
console.log(xhr.readyState);
52+
if (xhr.readyState === 4) {
53+
const data = JSON.parse(this.responseText);
54+
console.log(typeof data);
55+
console.log(data.followers);
56+
}
57+
};
58+
xhr.send();
59+
</script>
60+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
box-sizing: border-box;
5+
font-family: "Gilroy";
6+
}
7+
8+
html,
9+
body {
10+
width: 100%;
11+
height: 100%;
12+
background-color: #212121;
13+
color: #f1f1f1;
14+
}
15+
16+
#main {
17+
height: 100vh;
18+
width: 100%;
19+
display: flex;
20+
justify-content: center;
21+
align-items: center;
22+
}
23+
24+
th,
25+
td {
26+
border: 1px solid #f2f2f2;
27+
padding: 10px;
28+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
const promiseOne = new Promise(function (resolve, reject) {
2+
//Do an async task
3+
// DB calls, cryptography, network
4+
setTimeout(function () {
5+
console.log("Async task is completed!");
6+
resolve();
7+
}, 1000);
8+
});
9+
10+
// resolve is connected with `.then`
11+
promiseOne.then(function () {
12+
console.log("Promise consumed!");
13+
});
14+
15+
// new promise without storing function in variable
16+
new Promise(function (resolve, reject) {
17+
setTimeout(function () {
18+
console.log("Async task 2!");
19+
resolve();
20+
}, 1000);
21+
}).then(function () {
22+
console.log("Async 2 resolved!");
23+
});
24+
25+
// promise with storing function in variable and passing parameters to `.then`
26+
const promiseThree = new Promise(function (resolve, reject) {
27+
setTimeout(function () {
28+
resolve({ username: "Dummy", email: "dummy@example.com" });
29+
}, 1000);
30+
});
31+
32+
// passing `user` argument to `.then` from `promise` parameters
33+
promiseThree.then(function (user) {
34+
console.log(user);
35+
});
36+
37+
// promise with error (reject)
38+
const promiseFour = new Promise(function (resolve, reject) {
39+
setTimeout(function () {
40+
let error = true;
41+
if (!error) {
42+
resolve({ username: "chinmay", password: "123" });
43+
} else {
44+
reject("ERROR: Something went wrong");
45+
}
46+
}, 1000);
47+
});
48+
49+
// promise `.then` chaining with `.catch` and `.finally`
50+
promiseFour
51+
.then((user) => {
52+
console.log(user);
53+
return user.username;
54+
})
55+
.then((username) => {
56+
console.log(username);
57+
})
58+
.catch(function (error) {
59+
console.log(error);
60+
})
61+
.finally(() => console.log("The promise is either resolved or rejected"));
62+
63+
// handling promises with async functions
64+
const promiseFive = new Promise(function (resolve, reject) {
65+
setTimeout(function () {
66+
let error = true;
67+
if (!error) {
68+
resolve({ username: "javascript", password: "123" });
69+
} else {
70+
reject("ERROR: JS went wrong");
71+
}
72+
}, 1000);
73+
});
74+
75+
// handling promise with async function
76+
async function consumePromiseFive() {
77+
try {
78+
const response = await promiseFive;
79+
console.log(response);
80+
} catch (error) {
81+
console.log(error);
82+
}
83+
}
84+
85+
consumePromiseFive();
86+
87+
// async function getAllUsers() {
88+
// try {
89+
// const response = await fetch("https://jsonplaceholder.typicode.com/users");
90+
91+
// const data = await response.json();
92+
// console.log(data);
93+
// } catch (error) {
94+
// console.log("E: ", error);
95+
// }
96+
// }
97+
98+
// getAllUsers();
99+
100+
fetch("https://api.github.com/users/ChinmayKaitade")
101+
.then((response) => {
102+
return response.json();
103+
})
104+
.then((data) => {
105+
console.log(data);
106+
})
107+
.catch((error) => console.log(error));
108+
109+
// promise.all ✅
110+
// In JavaScript, `Promise.all` is a method that takes an array (or any iterable) of promises and returns a single promise that resolves when all of the promises in the array have resolved. If any of the promises in the array reject, the returned promise will immediately reject with that reason.
111+
112+
// How `Promise.all` Works:
113+
114+
// 1. Resolves when all promises resolve: If all the promises resolve, `Promise.all` resolves with an array of the resolved values, in the same order as the promises in the input array.
115+
116+
// 2. Rejects if any promise rejects: If any promise in the array rejects, `Promise.all` immediately rejects with the reason from the first promise that rejects, and ignores the results of the other promises.
117+
118+
// Example1:
119+
const promise1 = new Promise((resolve, reject) => {
120+
setTimeout(() => resolve("First Promise Resolved"), 1000);
121+
});
122+
123+
const promise2 = new Promise((resolve, reject) => {
124+
setTimeout(() => resolve("Second Promise Resolved"), 2000);
125+
});
126+
127+
const promise3 = new Promise((resolve, reject) => {
128+
setTimeout(() => resolve("Third Promise Resolved"), 1500);
129+
});
130+
131+
Promise.all([promise1, promise2, promise3])
132+
.then((results) => {
133+
console.log("All promises resolved:", results);
134+
})
135+
.catch((error) => {
136+
console.error("One of the promises rejected:", error);
137+
});
138+
139+
// Explanation:
140+
141+
// - Promises:
142+
// - `promise1` resolves after 1 second.
143+
// - `promise2` resolves after 2 seconds.
144+
// - `promise3` resolves after 1.5 seconds.
145+
146+
// - `Promise.all([promise1, promise2, promise3])`:
147+
// - Waits for all three promises to resolve.
148+
// - After 2 seconds (when the longest promise, `promise2`, resolves), the `then` block is executed.
149+
// - The `results` array contains: `['First Promise Resolved', 'Second Promise Resolved', 'Third Promise Resolved']`.
150+
151+
// Handling Rejections:
152+
153+
// If one of the promises rejects, `Promise.all` will immediately reject.
154+
const promise4 = new Promise((resolve, reject) => {
155+
setTimeout(() => resolve("First Promise Resolved"), 1000);
156+
});
157+
158+
const promise5 = new Promise((resolve, reject) => {
159+
setTimeout(() => reject("Second Promise Rejected"), 2000);
160+
});
161+
162+
const promise6 = new Promise((resolve, reject) => {
163+
setTimeout(() => resolve("Third Promise Resolved"), 1500);
164+
});
165+
166+
Promise.all([promise4, promise5, promise6])
167+
.then((results) => {
168+
console.log("All promises resolved:", results);
169+
})
170+
.catch((error) => {
171+
console.error("One of the promises rejected:", error);
172+
});
173+
174+
// In this case, after 2 seconds, `promise2` will reject, causing `Promise.all` to reject immediately with the error `'Second Promise Rejected'`. The other promises are ignored after the rejection.
175+
176+
// When to Use `Promise.all`:
177+
// - When you want to wait for multiple asynchronous operations to complete before proceeding.
178+
// - When you need all promises to succeed, and want to handle failure if any of them fail.
179+
180+
// This method is useful when you want to run multiple asynchronous tasks in parallel and wait for all of them to complete.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The Fetch API is finally coming to NodeJS 🚀🔥
2+
3+
- Author: Elijah Asaolu (March 3, 2022)
4+
- Site: LogRocket Article
5+
6+
Read this article.

0 commit comments

Comments
 (0)