DEV Community


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(`${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)