DEV Community

Discussion on: `throw` objects and functions

Collapse
citizen428 profile image
Michael Kohl

One gotcha here is, we generally use throw for exceptions and not in normal good case scenarios.

Ruby distinguishes the two use cases as begin/rescue (error handling) and throw/catch (control flow, i.e. exiting a nested loop).

The problem with your example is that the throw/catch construct is easily replaceable with a normal if statement. The actual React code you linked has no catch anywhere near the throw, so it will unwind the stack until it encounters the appropriate handler, something that is not easily achievable with a normal return statement (baring tuple returns and manual propagation of the same up the call stack).

Collapse
aravindballa profile image
Aravind Balla Author

That doesn't have a catch. But it handles it in the then part.

github.com/facebook/react/blob/mas...

Collapse
citizen428 profile image
Michael Kohl

Sure, but that's still very different from your example, which could have been just this:

let value;

// try reading from cache
// if not found, fetch it from external resource
value = read(key);
if (!value) {
  value = fakeExternalRes(key);
  store(key, value);
}

return value;

I understand that examples need to be simplified, I just feel that yours got simplified to the point where it's not actually showing what you intended to anymore.