JS Try-Catch Kinda Sucks (but Rust got it right)

April 9, 2025 (1 month ago)

less than a minute read

Alright, let's talk about something that fucks with me every time I write async code in JavaScript: try-catch.

It's not totally useless, but man, it feels clunky and jank. The scoping is weird as shit, you end up nesting a bunch of logic, and half the time you're not even sure what kind of error you're catching. It's like JavaScript just hands you a mystery box and says “good fucking luck”.

Meanwhile, over in Rust, error handling actually makes sense. They use this thing called Result<T, E>, and it basically forces you to deal with both success and failure cases explicitly. It's clean, it's predictable, and best of all, it feels like part of the language - not an afterthought.

Compare that to this classic JavaScript shit code:

try {
  const data = await doSomething();
  // do something else...
} catch (err) {
  // wait, what the fuck is this error?
}

Now you've got a bulky try-catch block, a floating variable scope, and code that gets messier the more logic you add. Quite confusing.

So here's a little helper function that has made my life a whole lot easier:

export async function safePromise<T, E = Error>(
  promise: Promise<T>,
): Promise<{ data: T | null; error: E | null }> {
  try {
    const data = await promise;
    return { data, error: null };
  } catch (error) {
    return { data: null, error: error as E };
  }
}

Now instead of writing try-catch blocks all over the place, I can just do this:

const { data, error } = await safePromise(fetchSomething());
 
if (error) {
  // handle the error properly
} else {
  // work with the data
}

Way cleaner. No more random nested blocks. No weird scoping. And it just feels so much nicer to use. You might also recognize this from TanStack Query/React Query if you have ever used something like that.

Is it as good as Rust's Result type? Not really but it's similar. It's one of those small changes that makes your code easier to read, test, and maintain.

Anyways, just wanted to share that. Little random things like this here and there keep me not going crazy when trying to write JavaScript code.

Thanks for reading this random rant.