DEV Community

artydev
artydev

Posted on

1 3

Handling errors with Fetch

Enhancement of one of my previous post Swiss&Fetch.
Here is a way to handling errors when fetch fails

async function fetchUsers(numUsers) {
    const response = await fetch(`https://randomuser.me/api/?results=${numUsers}`)
    return await response.json()
}

async function displayUsers(numUsers) {
    let data = await fetchUsers(numUsers).catch(e => console.log(e.message))
    data && listusers.setAttribute("users", JSON.stringify(data.results))
}

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay