-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const t = async (f) => { | |
| let output = undefined | |
| let error = undefined | |
| try { | |
| output = f() | |
| } catch(e) { | |
| error = e | |
| } | |
| return [output, error] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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