Question: How to recover from an error result to an ok result dynamically?
See original GitHub issueI see that I can map an ok value to any result I want by using .andThen
.
I can use unwrapOr
to replace every error by given value.
But what about mapping an error value to an ok value dymically? It’s sound quite useful that we want to recover after error and e.g. provide a default value for one type of error and left error as is for other type.
e.g.
const occupationResult: Result<string, NotFoundError | ConnectionError> = await getUserOccupation();
const safeOccupationResult = occupationResult.someErrorMappingFunction(
(error: NotFoundError | ConnectionError) => {
if (err instanceof NotFoundError) {
return ok("User not found");
} else {
return error;
}
}
);
// comparison to promises:
// a - rejected promise
const a = Promise.reject("an error");
// b - resolved promise
const b = a.catch(err => 33);
// c - rejected promise
const c = a.catch(err => Promise.reject("other error"));
how I could achieve currently the result that my imaginary function someErrorMappingFunction
do?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
sql - How to store results of a Dynamic Query in a temp table ...
My answer solves only one problem, storing the result of a dynamic proc call in a temp table. And there are more problems....
Read more >Dynamic query execution error - DBA Stack Exchange
The error you are receiving is because you have a trailing end; in your dynamic SQL version that is out of place (and...
Read more >An error occurred when sending commands to the program in ...
Method 3: Reset file associations · Right-click the Excel workbook, point to Open with, and then click More apps. · Select the version...
Read more >Dynamic Dynamic SQL - Ask TOM
Hi Tom, Looks like I got it figured out why it was not inserting any records.I suplied the values in the wrong order...
Read more >48 answers on StackOverflow to the most popular Angular ...
If the result of an HTTP request to a server or some other expensive ... To fix this error, you just need to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure I like the idea of having
map
andmapErr
accept callbacks returningResult
s. The point ofmap
andmapErr
is that they do not change the nature of theResult
(anOk
remains anOk
, anErr
remains anErr
), they should only transform the value itself.Promises are a bit different in that they auto-unwrap (
Promise<Promise<T>>
becomes aPromise<T>
) which is possible because they do not handle the error case.That’s why I believe
Result
should have distinctmap
mapErr
andThen
andorElse
as they all have a very specific transformation purpose. This is not specific toneverthrow
, we find the same distinct methods in most (all?) monad libs that have anEither
type.orElse
is now implemented:https://github.com/supermacro/neverthrow#resultorelse-method
Closing the issue to keep things a bit tidy, but feel free to chat here or in a new issue.