Skip to content

Instantly share code, notes, and snippets.

@ridvanaltun
Last active July 8, 2021 10:39
Show Gist options
  • Save ridvanaltun/6b7ec4fba45eb82dd276faaf0318079a to your computer and use it in GitHub Desktop.
Save ridvanaltun/6b7ec4fba45eb82dd276faaf0318079a to your computer and use it in GitHub Desktop.
A clever way to handling errors in async-await

logic

async function fetchData() {
  try{
    const data = await fetch()
    return [data, null]
  } catch(error) {
    return [null, error]
  }
}

usage

const [data, error] = await fetchData()

credit

Async Await try-catch hell: https://www.youtube.com/watch?v=ITogH7lJTyE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment