safe-await
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

Safe Await

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

Readme

Keywords

none

Package Sidebar

Install

npm i safe-await

Weekly Downloads

3,082

Version

3.0.1

License

MIT

Unpacked Size

4.71 kB

Total Files

4

Last publish

Collaborators

  • davidwells