-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises_JOKES.HTML
80 lines (66 loc) · 2 KB
/
promises_JOKES.HTML
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jokes</title>
<style>
body {
background-color: black;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
.joke {
width: 20vw;
height: 5vw;
margin-bottom: 2vw;
color: black;
background-color: white;
}
#btn {
color: black;
}
* {
color: white;
}
</style>
</head>
<body>
<div>
<h3>Joke :</h3>
<div class="joke" id="joke">Joke is loading...</div>
<button id="btn">next</button>
</div>
<script>
// promises:
// fetch(/*link of api*/); // returns the promise.
// 90% we will consume promise, in advance we create promise.
const joke = document.querySelector("#joke");
const jokeBtn = document.querySelector("#btn");
const generateJoke = () => {
// then and catch runs like try and catch.
const setHeader = () => {
Accept: "application/json";
};
fetch('https://icanhazdadjoke.com', setHeader)
.then((response) => {
// response in html format, we nned to convert to JSON form.
response.json();
})
.then((data) => {
joke.innerHTML = data.joke;
})
.catch((error) => {
alert("API error!!!!......");
})
};
jokeBtn.addEventListener('click', generateJoke);
generateJoke();
</script>
</body>
</html>