flat-try
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Flat try

An abstraction to handle exceptions without the need of new code blocks, improving readability (IMO)

Works for both sync and async functions

Install

npm i --save flat-try

Usage

Functions:

const Try = require('flat-try')

const [err, result] = Try(() => myDangerousFunc(arg1))

if (err) {
    // handle it
}

// use the result

If the function context is not relevant, there is no need to wrap it with arrow functions, just use apply:

const Try = require('flat-try')

const [err, result] = Try.apply(myDangerousFunc, arg1, arg2, argN)

if (err) {
    // handle it
}

// use the result

Promises/Async:

const Try = require('flat-try')

const [err, result] = await Try.promise(myDangerousFuncThatReturnsAPromise(arg1))

if (err) {
    // handle it
}

// use the result

Inspired by: safe-await

Shout-out to: BPatinho

Readme

Keywords

Package Sidebar

Install

npm i flat-try

Weekly Downloads

2

Version

1.1.1

License

MIT

Unpacked Size

5.14 kB

Total Files

6

Last publish

Collaborators

  • tarcisiozf