1.1.1 • Published 3 years ago

flat-try v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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