/flat-try

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

Primary LanguageJavaScriptMIT LicenseMIT

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 you don't worry about the function context you don't 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