Skip to content

Instantly share code, notes, and snippets.

@felquis
Last active March 4, 2026 19:01
Show Gist options
  • Select an option

  • Save felquis/3a6c98574adcaeecd0b255e2d60a16c3 to your computer and use it in GitHub Desktop.

Select an option

Save felquis/3a6c98574adcaeecd0b255e2d60a16c3 to your computer and use it in GitHub Desktop.
executes a function and return go like array, first success return, second the error if any (inspired by https://www.npmjs.com/package/try)
export const t = async (f) => {
let output = undefined
let error = undefined
try {
output = f()
} catch(e) {
error = e
}
return [output, error]
}
import { t } from './t.js'
const main = async () => {
const goodFunk = () => 'good'
const badFunk = () => 'bad'.dontHaveAMethod()
const [goodOutput, err] = await t(goodFunk)
if (!err) console.log(goodOutput)
const [badOutput, badError] = await t(badFunk)
if (badError) console.log('We need to talk about the bad funk')
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment