0.0.1 • Published 6 years ago
omega-fn v0.0.1
Ω: 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-fnUsage
const Ω = require('omega-fn')
const [err, result] = await Ω (fnThatMayThrowError, arg1, arg2, ...)Rationale
This is annoying:
let result
try {
result = await fnThatMayThrowError()
}
catch (err) {
// ...
}
// use resultThis is nicer:
let [err, result] = await Ω (fnThatMayThrowError)
if (err) {
// ...
}
// use resultWhat'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 afinallyblock- 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')
0.0.1
6 years ago