DEV Community

artydev
artydev

Posted on

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))
}

Discussion (0)