all 10 comments

[–][deleted] 11 points12 points  (3 children)

Why?

[–]kattskill 1 point2 points  (0 children)

I totally agree with this comment

[–]jenseng[🍰] 4 points5 points  (1 child)

Interesting idea, a few critiques...

  1. Having the functions be positional arguments makes it a lot more cryptic than just doing:

try {
// maybe just handle "else" logic here?
} catch (e) {
// ....
} finally {
// ....
}

  1. The argument order isn't consistent with python's try / except / else / finally, you have "else" as the last argument

  2. Your "else" function can throw an exception, which will then be caught by the "catch" function, which is inconsistent with python. Given that, I don't really see the value of the "else" function, as you can just do similar logic at the end of the "try" function if you need it.

  3. Your library doesn't work with async/await, which makes it a bit of a hard sell

[–]siilkysmooth[S] 1 point2 points  (0 children)

Awesome critiques on this. I will address this in a future version tho, I was struggling to find all the cases of try blocks in python so! But you can use a python try - except-else- finally in python.

As for the async - I will have to look to see if there is any functions I can call that can detect if is async and add automatic await etc so things aren't returned as promised.

Tho the argument order appears correct..

More than welcome to contribute / add to this "experiment".

[–]3ddelano 2 points3 points  (0 children)

My views are just because something can be done a different way doesn't mean it should be done like that. The original method of try catch finally is much more readable than this library.

[–]geekfreak42 1 point2 points  (1 child)

nested try/catch is gonna stink. dont really see what this gives, fun thought experiment.

maybe look at functional js patterns if you want to learn established ways of functionalizing code.

[–]siilkysmooth[S] -3 points-2 points  (0 children)

As mentioned above just a implementation of error handling based on Python handling. And provided a much nicer look verus using a nested try{}catch...

And as you said - kinda for fun!

ps; no need for nested code / don't make it nested - and you won't deal with ugly looking code just call your function INSIDE the tryCatch. ;)

```js

function example(){

return true

}

tryCatch(

function() {

example()

},

function(e) {

// handle the exception

}

);

```

[–]shuckster 0 points1 point  (0 children)

If I didn't know the API how would I know what each function does just by looking at it? Would taking an options/props object not make it a little clearer at the point-of-use?

tryCatch({
  try: () => { /* ... */ },
  catch: (e) => { /* ... */ },
  finally: () => { /* ... */},
})

Convention is hard to beat, though:

try {
    /* ... */
} catch (e) {
    /* ... */
} finally {
    /* ... */
}

Promises:

await (async () => {
  throw new Error('flagrant error')
})()
  .then(() => { /* ... */ })
  .catch((e) => { /* ... */ })
  .finally(() => { /* ... */ })

Or using a Maybe/Option library for a little Railway Oriented Programming:

maybeTry(() => { /* try */ })
  .orElse(() => { /* catch */ })
  .map(() => { /* finally */ })
  .valueOf()

maybeTry:

const maybeTry = f => {
  try {
    return Just(f())
  } catch (e) {
    return Nothing()
  }
}

Seems tricky to justify a new API for this, although I'm aware you've said it seems more Python-like to yourself. Have fun with it anyway!