Skip to content

Commit b19c286

Browse files
committed
Fetch with promise
1 parent 29b7851 commit b19c286

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

fetch/fetch-promise/enhanced-fetch.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const output = document.getElementById("output");
2+
const btn = document.getElementById("btn");
3+
const input = document.getElementById("input");
4+
5+
const url = fetch("https://randomuser.me/api");
6+
7+
const fetchAPI = () => {
8+
url
9+
.then(
10+
(response) => {
11+
if (!response.ok) throw response.statusText;
12+
return response.clone().json();
13+
},
14+
(err) => {
15+
//! first level catch/interrupt
16+
//the fetch failing
17+
console.log("server error");
18+
}
19+
)
20+
.then(
21+
(data) => {
22+
console.log(data);
23+
},
24+
(err) => {
25+
//! failed to run response.json()
26+
console.log(" error");
27+
}
28+
)
29+
.catch((err) => {
30+
console.log("network error");
31+
});
32+
};
33+
btn.addEventListener("click", fetchAPI);

0 commit comments

Comments
 (0)