Skip to content

aiwebb/bimodal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bimodal

bimodal is an experiment in replacing try/catch boilerplate with an [err, result] array, inspired by error return values in Go.

Installation

# npm
npm i --save bimodal

# yarn
yarn add bimodal

Usage

const bimodal = require('bimodal')

const [err, result] = await bimodal (fnThatMayThrowError, arg1, arg2, ...)

Rationale

This is annoying:

let result

try {
  result = await fnThatMayThrowError()
}
catch (err) {
  // ...
}

// use result

This is nicer:

let [err, result] = await bimodal (fnThatMayThrowError)

if (err) {
  // ...
}

// use result

What's it doing?

Not much:

module.exports = async (fn, ...args) => {
  const channels = [undefined, undefined]

  try {
    channels[1] = await fn.apply(null, args)
  }
  catch (err) {
    channels[0] = err
  }
  finally {
    return channels
  }
}

About

An experiment in replacing try/catch boilerplate with an [err, result] array

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published