File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 ) ;
You can’t perform that action at this time.
0 commit comments