Skip to content

devlzcode/to-easy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to-easy

NPM version Downloads License

Elegant wrapper for error handling both synchronous and asynchronous functions. Inspired by await-to-js

Pre-requisites

You need to use Node 7.6 (or later) or an ES7 transpiler in order to use async/await functionality. You can use babel or typescript for that.

Install

#npm
npm install to-easy

# yarn
yarn add to-easy

Usage

import { asyncTo, syncTo } from 'to-easy'

const isEven = async (n: number) => {
    if (n % 2 === 0) return true
    else throw new Error('Number is not even')
}

const isOdd = (n: number) => {
    if (n % 2 !== 0)  return true
    else throw new Error('Number is not odd')
}

const checkIfNumberIsEven = async (n: number) => {
    const [e, result] = await asyncTo(isEven(n))
    if (!result) console.error(e.message)
    else console.log(`${n} is even!`)
}

const checkIfNumberIsOdd = (n: number) => {
    const [e, result] = syncTo(isOdd, n)
    if (!result) console.error(e.message)
    else console.log(`${n} is odd!`)
}

checkIfNumberIsEven(2) // 2 is even!
checkIfNumberIsOdd(2) // Number is not odd

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published