Skip to content

tarcisiozf/flat-try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published