dpes8693 / frontend-mentor-program-1rd

前端培訓
0 stars 1 forks source link

詹珀柚_HW7&Question #56

Open JamesChan0107 opened 2 months ago

JamesChan0107 commented 2 months ago
dpes8693 commented 2 months ago
dpes8693 commented 2 months ago
  • HW7:https://jameschan0107.github.io/myfirstcallapi
  • Q:以下是我在fetch的部分是使用async/await的方式去做try catch,想說看起來簡潔點,但想請問會建議還是使用教材上的做法嗎,還是根本沒差?
async function fetchContestData(page) {
    try {
        const response = await fetch(`https://memes.tw/wtf/api?contest=621&page=${page}`);
        const data = await response.json();
        return data;
    } catch (error) {
        console.error(`Error fetching data: ${error}`);
    }
}

await then 兩種做法都OK 如果會出現巢狀建議是用await 比較簡潔

ex: 有一個情境是需要call 3隻API 像是排隊那樣 先call A, 再call B, 再call C 這種寫then就不好閱讀