neverthrow | Type-Safe Errors for JS & TypeScript | Reactive Programming library
kandi X-RAY | neverthrow Summary
kandi X-RAY | neverthrow Summary
Encode failure into your program. This package contains a Result type that represents either success (Ok) or failure (Err). For asynchronous tasks, neverthrow offers a ResultAsync class which wraps a Promise> and gives you the same level of expressivity and control as a regular Result. ResultAsync is thenable meaning it behaves exactly like a native Promise ... except you have access to the same methods that Result provides without having to await or .then the promise! Check out the wiki for examples and best practices. Need to see real-life examples of how to leverage this package for error handling? See this repo:
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of neverthrow
neverthrow Key Features
neverthrow Examples and Code Snippets
Community Discussions
Trending Discussions on neverthrow
QUESTION
After I had used throw and catch throughout my whole typescript/node application at first, I stumbled over neverthrow(https://www.npmjs.com/package/neverthrow).
It took me to write my first, larger application and typescript, to realize what a big issue the missing ability to mark a function as throwing actually is.
neverthrow or other either based solutions seem just fine. For my non-Async functions I have already changed everything to neverthrow.
I don't really see the point, why I should bother to wrap all my async functions to return ResultAsync though. Any async function is marked as possibly throwing an error by nature(as they return a Promise), or am I wrong? Is there any point I missing and I should indeed change all async functions to use ResultAsync?
...ANSWER
Answered 2021-Oct-28 at 13:55There's few benefits to using something like the ResultAsync
type instead of a Promise
.
As can be seen from the type itself, the former option has a E
generic parameter indicating the types of errors that the result may contain. This is hugely beneficial, as you can deal with each error type independently instead of catching a generic error instance. Take for example HTMLImageElement.decode; it returns a promise that might throw EncodingError
, but you wouldn't know that without checking documentation.
Another benefit comes from being forced to deal with the potential error result. Javascript errors are unchecked, meaning that there is no compiler or interpreter to ensure that you always wrap await promise
in a try-catch. On the other hand, when you await result
, you must check whether it yielded a result (result.isOk() === true
) or threw an error (result.isErr() === true
). This means that every time you want to unwrap (get T
from Result
) a result, you must deal with the potential error cases. In theory, this results in more robust and fail-safe code.
Result does not come without its downsides though. Javascript ecosystem has adopted Promises as the standard way to return things asynchronously, so ResultAsync
may not work everywhere Promise
would. Also, having to deal with potential error results every time you want to unwrap a result makes code more verbose.
If you're mostly dealing with async code written within the same project and think you could benefit from more fail-safe code, it may make sense to use ResultAsync
everywhere you'd use Promise
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install neverthrow
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page