-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (40 loc) · 1.19 KB
/
app.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
let body = document.querySelector("body");
body.style.backgroundColor = "gray";
// ---------------- fetching data using - Axios library -----------------
let url = "https://catfact.ninja/fact";
let btn = document.querySelector("button");
btn.addEventListener("click",async ()=>{
console.log("btn clicked");
let fact = await getFact();
console.log(fact);
let p = document.querySelector("#result");
// p.append(fact) ;
p.innerHTML = `<hr> `;
p.append(fact) ;
// p.innerHTML = `<hr> ${fact}`;
})
async function getFact() {
try {
let res = await axios(url);
return (res.data.fact);
} catch (e) {
console.log("errorHERE - ", e);
return "No Fact Found"
}
}
// getFact();
// ------------ parsing with js --------------------
// let url = "https://catfact.ninja/fact";
// async function getFact() {
// try {
// let res = await fetch(url);
// let data = await res.json();
// console.log(data.fact);
// let res2 = await fetch(url);
// let data2 = await res2.json();
// console.log(data2.fact);
// } catch (e) {
// console.log("errorHERE - ", e);
// }
// }
// getFact();