Guarantees on promise rejection
See original GitHub issueHi, the README.md carefully advises against having exceptions/rejections when working with the library.
In particular, the fromSafePromise
function mentions:
Ensure you know what you’re doing, otherwise a thrown exception within this promise will break any guarantees that neverthrow provides.
Now, I would like to encode expected/regular errors in the Result type, but allow unexpected/rare/fatal errors to reject my promises.
Looking at the code, this library seems to allow that, but the documentation kind of suggests that that is not okay and the compatibility could break later on after some changes.
What is the position on that? I can always not use ResultAsync
and encode my functions as Promise<Result<T,E>>
manually, but it is kind of a pity, because ResultAsync
methods are pretty useful.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
I think so! I’ll try to sketch something next week. Thanks!
Hey @davazp,
Great question, and something I should probably bring up more explicitly in the docs.
I think there isn’t a clear-cut answer and it’s about a team’s / individual’s preference on how they want to deal with / think about “irrecoverable errors”.
I would say that most people in the functional programming community (and Rust?) would agree with me when I say that you should encode recoverable errors into your type system (i.e. in your
Result
types), and have a separate process / system in place for irrecoverable errors.For servers, that means - shut down your server because something horribly wrong has just occurred and your server is now in a non-sensical state. An example of this would be if you’re missing critical environment variables. In some of my applications, I
throw
and have aprocess.on('UnhandledException', () => {})
handler that terminates the server safely.The rust community has done a great job of documenting this distinction between recoverable vs irrecoverable errors:
https://doc.rust-lang.org/stable/book/ch09-00-error-handling.html