Browse Source

error catching added. buttons loading text added

miskson 3 years ago
parent
commit
f23fbe976d
1 changed files with 14 additions and 17 deletions
  1. 14 17
      hw11-promise/script.js

+ 14 - 17
hw11-promise/script.js

@@ -37,7 +37,8 @@ function drawObjTable(parent, obj) {
                 i++;
                 console.log('current', item)
                 if (typeof item === 'string' && /^(https:\/\/|http:\/\/)/.test(item)) {
-                    let btn = createBtn('Loading...')
+                    let btn = createBtn(`Loading...`)
+                    btn.disabled = true
 
                     
                     fetch(item)
@@ -50,15 +51,11 @@ function drawObjTable(parent, obj) {
                         
                         })
                         .then(obj => {
+                            btn.disabled = false
                             btn.innerText = obj[Object.keys(obj)[0]]
                             btn.onclick = () => { drawObjTable(container, obj) }
                         })
-                        .catch((e) => {
-                            console.warn('something bad happened. ',e)    
-                            btn = document.createElement('a')
-                            btn.innerText =  item
-                            btn.href = item
-                        })
+                        .catch(e => console.warn('something bad happened ', e))
 
                     tdValue.append(btn)
                 } else {
@@ -67,16 +64,16 @@ function drawObjTable(parent, obj) {
             }
         } else if (typeof obj[key] === 'string' && /^(https:\/\/|http:\/\/)/.test(obj[key])) {
             let btn = createBtn(key)
-            try {
-                fetch(obj[key])
-                    .then(res => res.json())
-                    .then(obj => {
-                        btn.innerText = obj[Object.keys(obj)[0]]
-                        btn.onclick = () => { drawObjTable(container, obj) }
-                    })
-            } catch(e) {
-                console.warn('something bad happened. ',e)
-            }
+            btn.disabled = true
+        
+            fetch(obj[key])
+                .then(res => res.json())
+                .then(obj => {
+                    btn.disabled = false
+                    btn.innerText = obj[Object.keys(obj)[0]]
+                    btn.onclick = () => { drawObjTable(container, obj) }
+                })
+                .catch(e => console.warn('something bad happened ', e))
 
             tdValue.append(btn)
         } else {