1.0.0 • Published 3 years ago

to-easy v1.0.0

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

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
1.0.0

3 years ago

1.0.1

3 years ago