Skip to content

Instantly share code, notes, and snippets.

@max
Last active March 23, 2019 19:34
Show Gist options
  • Save max/3c8d8c215736471aa40934203a7eeed3 to your computer and use it in GitHub Desktop.
Save max/3c8d8c215736471aa40934203a7eeed3 to your computer and use it in GitHub Desktop.

The error handling for async/await with try/catch always bugs me. Go to the rescue?

function to(promise) {
  return promise
    .then(res => [null, data])
    .catch(err => [err])
}

async function doIt() {
  const [err, data] = await to(request("https://jsonplaceholder.typicode.com/posts"))
  if (err) return console.error("Dat error though...")
  return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment