Skip to content

Commit a0a590f

Browse files
committed
Fetch API
1 parent 22e2a4e commit a0a590f

File tree

5 files changed

+366
-0
lines changed

5 files changed

+366
-0
lines changed

16. Fetch API/data.txt

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"persons": [
3+
{
4+
"name": "Rahim",
5+
"age": 25,
6+
"hometown": "Dhaka",
7+
"married": false
8+
},
9+
{
10+
"name": "Karim",
11+
"age": 35,
12+
"hometown": "Chittagong",
13+
"married": true
14+
},
15+
{
16+
"name": "Ayon",
17+
"age": 27,
18+
"hometown": "Chandpur",
19+
"married": false
20+
},
21+
{
22+
"name": "Rahat",
23+
"age": 34,
24+
"hometown": "Chittagong",
25+
"married": true
26+
},
27+
{
28+
"name": "Rony",
29+
"age": 26,
30+
"hometown": "Chittagong",
31+
"married": false
32+
}
33+
],
34+
35+
"students": [
36+
{
37+
"name": "Abdullah",
38+
"age": 25,
39+
"hometown": "Dhaka",
40+
"married": false
41+
},
42+
{
43+
"name": "Al-Amin",
44+
"age": 35,
45+
"hometown": "Chittagong",
46+
"married": true
47+
},
48+
{
49+
"name": "Abdur Rahman",
50+
"age": 27,
51+
"hometown": "Chandpur",
52+
"married": false
53+
},
54+
{
55+
"name": "Obaydur Rahman",
56+
"age": 34,
57+
"hometown": "Chittagong",
58+
"married": true
59+
},
60+
{
61+
"name": "Abu Hanifa",
62+
"age": 26,
63+
"hometown": "Chittagong",
64+
"married": false
65+
}
66+
]
67+
}

16. Fetch API/img/image.png

357 KB
Loading

16. Fetch API/index.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Java Script Basics</title>
8+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
9+
<style>
10+
* {
11+
margin: 0;
12+
padding: 0;
13+
}
14+
</style>
15+
</head>
16+
17+
<body>
18+
19+
<div class="container">
20+
<div class="row">
21+
<div class="col-6">
22+
<p class="fs-4" id="output"></p>
23+
<h3>Promises</h3>
24+
<p class="fs-4" id="prom_out"></p>
25+
<p class="fs-4" id="demo"></p>
26+
</div>
27+
</div>
28+
<div class="row">
29+
<div class="col-6">
30+
<button class="btn btn-warning" id="get_jokes">GET JOKES</button>
31+
<p class="fs-4" id="showJokes"></p>
32+
</div>
33+
</div>
34+
</div>
35+
36+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
37+
<script src="js/script.js"></script>
38+
<p></p>
39+
</body>
40+
41+
</html>

16. Fetch API/js/script.js

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// Set setTimeout function
2+
// Set timeout function wait number of milliseconds for execute but at the same time
3+
// other program are run
4+
// setTimeout function call automatically
5+
6+
setTimeout(function () {
7+
console.log("Hello world!");
8+
}, 3000);
9+
10+
function getMessage() {
11+
setTimeout(function () {
12+
console.log("Message");
13+
}, 4000);
14+
}
15+
16+
function getMessageLong() {
17+
setTimeout(function () {
18+
console.log("Message After Long Time");
19+
}, 1000);
20+
}
21+
22+
23+
24+
getMessage();
25+
getMessageLong();
26+
// console.log("Other Progam are running");
27+
28+
29+
// let persons = [
30+
// {firstName: 'Abdullah', lastName: 'Abdur Rahnam'},
31+
// {firstName: 'Obaydur', lastName: 'Rahman'}
32+
// ]
33+
34+
// function getPerson(persons){
35+
// let output="";
36+
// setTimeout(function(){
37+
// persons.forEach(function(value){
38+
// output += value.firstName + " " + value.lastName + "<br>";
39+
// });
40+
// document.getElementById("output").innerHTML = output;
41+
// },1000)
42+
// }
43+
44+
// function addPerson(person){
45+
// setTimeout(function(){
46+
// persons.push(person);
47+
// },4000)
48+
// }
49+
// // New new elment not pring here because of sychronous behaviour in prgramming. For eliminate it we need to asychronous programming
50+
// addPerson({firstName: 'Abul', lastName: 'Kalam'});
51+
// getPerson(persons)
52+
53+
54+
console.log("Other Progam are running");
55+
56+
57+
let persons = [
58+
{ firstName: 'Abdullah', lastName: 'Abdur Rahnam' },
59+
{ firstName: 'Obaydur', lastName: 'Rahman' }
60+
]
61+
62+
function getPerson() {
63+
let output = "";
64+
setTimeout(function () {
65+
persons.forEach(function (value) {
66+
output += value.firstName + " " + value.lastName + "<br>";
67+
});
68+
document.getElementById("output").innerHTML = output;
69+
}, 500)
70+
}
71+
72+
function addPerson(person, getPerson) {
73+
setTimeout(function () {
74+
persons.push(person);
75+
getPerson();
76+
}, 2000)
77+
}
78+
//Asychronous way to solve the problem
79+
addPerson({ firstName: 'Abul', lastName: 'Kalam' }, getPerson);
80+
81+
82+
// Promises In Js
83+
// "Producing code" is code that can take some time
84+
// "Consuming code" is code that must wait for the result
85+
// A Promise is an Object that links Producing code and Consuming code
86+
87+
// JavaScript Promise Object
88+
// A Promise contains both the producing code and calls to the consuming code:
89+
90+
// Syntax
91+
// let myPromise = new Promise(function(myResolve, myReject) {
92+
// // "Producing Code" (May take some time)
93+
94+
// myResolve(); // when successful
95+
// myReject(); // when error
96+
// });
97+
98+
// // "Consuming Code" (Must wait for a fulfilled Promise)
99+
// myPromise.then(
100+
// function(value) { /* code if successful */ },
101+
// function(error) { /* code if some error */ }
102+
// );
103+
104+
105+
// When the producing code obtains the result, it should call one of the two callbacks:
106+
107+
// Promise Object Properties
108+
// A JavaScript Promise object can be:
109+
// Pending = undefined
110+
// Fulfilled = a result value
111+
// Rejected = an error object
112+
113+
// Promise How To
114+
// Here is how to use a Promise:
115+
// myPromise.then(
116+
// function(value) { /* code if successful */ },
117+
// function(error) { /* code if some error */ }
118+
// );
119+
120+
// Ex:
121+
let myPromise = new Promise(function (myResolve, myReject) {
122+
setTimeout(function () { myResolve("I love You !!"); }, 3000);
123+
});
124+
125+
myPromise.then(function (value) {
126+
document.getElementById("prom_out").innerHTML = value;
127+
});
128+
129+
130+
131+
function getPerson() {
132+
let output = "";
133+
setTimeout(function () {
134+
persons.forEach(function (value) {
135+
output += value.firstName + " " + value.lastName + "<br>";
136+
});
137+
document.getElementById("output").innerHTML = output;
138+
}, 500)
139+
}
140+
141+
function addPerson(person, getPerson) {
142+
let prom = new Promise(function (resolve, reject) {
143+
let state = false;
144+
145+
if (state == false) {
146+
resolve();
147+
} else {
148+
reject(new Error("Promise not accepted"));
149+
}
150+
persons.push(person);
151+
//If resolve function can called it executes the promise successfully
152+
153+
});
154+
155+
return prom;
156+
}
157+
//Asychronous way to solve the problem
158+
addPerson({ firstName: 'Abu', lastName: 'Huraira' }).then(getPerson).catch(function (error) {
159+
console.error(error.message);
160+
});
161+
162+
// Example using Promise: Waiting for a file
163+
164+
// let prom = new Promise(function(myResolve, myReject){
165+
// let req = new XMLHttpRequest();
166+
// req.open('GET', 'car.txt', true);
167+
168+
// req.onload = function(){
169+
// if(req.status===200){
170+
// myResolve(req.responseText);
171+
// }else{
172+
// myReject(new Error("Promise not accepted"));
173+
// }
174+
// }
175+
176+
// })
177+
178+
// function myDisplayer(some) {
179+
// document.getElementById("prom_out2").innerHTML = some;
180+
// }
181+
182+
// prom
183+
184+
// function myDisplayer(some) {
185+
// document.getElementById("demo").innerHTML = some;
186+
// }
187+
188+
// let myPromise2 = new Promise(function(myResolve, myReject) {
189+
// let req = new XMLHttpRequest();
190+
// req.open('GET', "myCar.html",true);
191+
// req.onload = function() {
192+
// if (req.status == 200) {
193+
// myResolve(req.response);
194+
// } else {
195+
// myReject("File not Found");
196+
// }
197+
// };
198+
// req.send();
199+
// });
200+
201+
// myPromise2.then(
202+
// function(value) {myDisplayer(value);},
203+
// function(error) {myDisplayer(error);}
204+
// );
205+
206+
207+
208+
//Fetch API
209+
// Fetch like AJAX, this uses javascript promise
210+
211+
document.getElementById('get_jokes').addEventListener('click', getJokes);
212+
213+
function getJokes() {
214+
//fetch function returns a promise thats why we called then
215+
fetch('https://api.api-ninjas.com/v1/jokes/', {
216+
headers: {
217+
'X-Api-Key': 'f88n0jSzxJ9hdDCDlU8p/A==wrX6uj9eqGVhzao2'
218+
}
219+
})
220+
.then(response => response.json())
221+
.then(data => document.getElementById('showJokes').innerText=(data[0].joke))
222+
.catch(err => document.getElementById('showJokes').innerText=err.message)
223+
}

16. Fetch API/myCar.html

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Car</title>
8+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
9+
<style>
10+
* {
11+
margin: 0;
12+
padding: 0;
13+
}
14+
</style>
15+
</head>
16+
17+
<body>
18+
19+
<div class="container">
20+
<div class="row">
21+
<div class="col-6">
22+
<img class="img-fluid" src="img/image.png" alt="">
23+
<p class="fs-5">A car is a wheeled, self-powered motor vehicle used for transportation. Most definitions of the term specify that cars are designed to run primarily on roads, to have seating for one to eight people, to typically have four wheels.
24+
<br>
25+
(Wikipedia)</p>
26+
</div>
27+
</div>
28+
</div>
29+
30+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
31+
<script src="js/script.js"></script>
32+
<p></p>
33+
</body>
34+
35+
</html>

0 commit comments

Comments
 (0)