omega-fn
v0.0.1
Published
Ω eliminates try/catch boilerplate and replaces it with a two-channel [err, result] array
Downloads
6
Maintainers
Readme
Ω: the Omega function
Ω
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 omega-fn
# yarn
yarn add omega-fn
Usage
const Ω = require('omega-fn')
const [err, result] = await Ω (fnThatMayThrowError, arg1, arg2, ...)
Rationale
This is annoying:
let result
try {
result = await fnThatMayThrowError()
}
catch (err) {
// ...
}
// use result
This is nicer:
let [err, result] = await Ω (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
}
}
Why Ω?
Ω
represents finality, and this function is conceptually similar to afinally
block- it's keyboard-friendly (at least on a Mac:
option
+z
) - since the goal is hiding as much annoying syntax as possible, I wanted something that was only a single character
- unclaimed single-char non-alpha keyboard-friendly valid JS identifiers are in very short supply (thanks a lot,
$
and_
)
If you're weirded out by it, you can always call it something else:
const omega = require('omega-fn')
const channels = require('omega-fn')
const potato = require('omega-fn')