Hacker News new | ask | show | jobs
by notheguyouthink 2881 days ago
What's the `err` being returned here? I didn't think JavaScript was big on error returning, as might typically be used by a `returnif` keyword.

This, funny enough, reminds me of Go. We've been having debates about how to improve syntax of `if err != nil { return err }`, and many of us (myself included) disliked helpers to immediately return or hide an error value.

The reason is that `if err != nil { return err }` is actually somewhat non-idiomatic. It returns the error value with no additional context of where the error occurred, what is wrong, etc. So something like `returnif` in Go, I think is bad. For reference, this is a more idiomatic example: `if err != nil { return fmt.Errorf("foo bar: %v",err) }` where `foo bar` adds context about the thing returning the error.

So perhaps my Go logic doesn't apply here in JS land, but it seems like a bad keyword, if it's similar to Go.

1 comments

The proposed syntax allows for a precondition which can evaluate an expression and return that.

So if you wanted to format the error returned you could do `returnif error : formatError("Error context:", error)`