resn

    1.1.0 • Public • Published

    resn

    Go-style normalized errors in JS

    GitHub | NPM

    Install

    npm i resn
    const resn = require('resn')

    Use

    Here, we pass in a callback function (fs.readFile) to be converted to a resn callable.

    const rReadFile = resn(fs.readFile)
     
    ;(async () => {
        var {err, res} = await rReadFile('index.js')
     
        if (err) {
            console.error('[!]', err.message)
            return
        }
     
        console.log('[~] Read file!')
        console.log(res.toString())
    })()

    We can pass in all kinds of things to make resn callables. We can convert almost all libraries' functions, new and old, to resn callables.

    const myPromFunc = () => {
        return new Promise((res, rej) => {
            res('Hey!')
        })
    }
     
    const rPromFunc = resn(myPromFunc)
     
    const basicFunc = function (giveError = false) {
        if (giveError) {
            throw new Error('Failed!')
        }
     
        return 'Hey!'
    }
     
    const rBasicFunc = resn(basicFunc)

    We can even convert a whole library of varying-style functions to resn callables at once.

    const fs = resn(require('fs'))
     
    ;(async () => {
        var {err, res} = await fs.readFile(__filename)
     
        // ...
    })

    Why?

    The resn method of handling errors is effective because it avoids problems associated with old style handling. We can avoid using try/catch constantly and can gracefully handle all errors. Also, we can convert all kinds of functions into a common format easily with one function.

    It is recommended that you use var when receiving resn callable responses as this will allow you to reuse the err and res variable names within a scope.

    Install

    npm i resn

    DownloadsWeekly Downloads

    4

    Version

    1.1.0

    License

    MIT

    Unpacked Size

    5.18 kB

    Total Files

    4

    Last publish

    Collaborators

    • ethan_