safe-await is a tiny helper for making async control flow easier to read.

Instead of wrapping every awaited call in its own try/catch block, it returns a tuple-style result that makes the success and error paths explicit.

Why it matters

Small utilities like this reduce repeated code and make common JavaScript patterns easier to reuse consistently across projects.

Full documentation, synced from the package README:

Safely use async/await without all the try catch blocks

Usage

const safeAwait = require('safe-await')

async function fooBar() {
  const [error, data] = await safeAwait(promiseOne())

  if (error) {
    // handle error, retry, ignore, whatever
  }

  console.log(data)
}

See usage and tests for more examples.

Invert

You can invert the error and data return values by passing true as the second argument.

const safeAwait = require('safe-await')

async function fooBar() {
  const invert = true
  const [data, error] = await safeAwait(promiseOne(), invert)

  if (error) {
    // handle error, retry, ignore, whatever
  }

  console.log(data)
}

Alternatively, you can use the safeInverse function.

const { safeInverse } = require('safe-await')

async function fooBar() {
  const [data, error] = await safeInverse(promiseOne())
  
  if (error) {
    // handle error, retry, ignore, whatever
  }

  console.log(data)
}

Research

Other libraries

Other handy error utils