Open plouj opened 2 years ago
I guess a bit more context is useful here from https://github.com/scala/docs.scala-lang/blob/6ef51ab1677fa1f7c311d06db164911d08bedc5c/_overviews/core/futures.md?plain=1#L620
The
recover
combinator creates a new future which holds the same result as the original future if it completed successfully. If it did not then the partial function argument is applied to theThrowable
which failed the original future. If it maps theThrowable
to some value, then the new future is successfully completed with that value. If the partial function is not defined on thatThrowable
, then the resulting future is failed with the sameThrowable
.The
recoverWith
combinator creates a new future which holds the same result as the original future if it completed successfully. Otherwise, the partial function is applied to theThrowable
which failed the original future. If it maps theThrowable
to some future, then this future is completed with the result of that future. Its relation torecover
is similar to that offlatMap
tomap
.
Thanks for posting this. Had a nice read seeing functional programming principles in action.
However, I feel the effort the somebody would spend understanding the difference here, will get them much further in solidifying their knowledge how functional programming is getting applied to real scenarios.
Thank you @plouj for the report. I agree it would be good to highlight the differences between recover
and recoverWith
in their documentation. Would you be interested in submitting a PR?
@plouj pls go ahead with aPR
Right now https://github.com/scala/docs.scala-lang/blob/6ef51ab1677fa1f7c311d06db164911d08bedc5c/_overviews/core/futures.md?plain=1#L628 starts the same for both
recover
andrecoverWith
:vs
which is rather confusing to beginners.
The first real differentiation appears in the middle of the paragraphs:
vs
which took me a while to find and I personally struggled to parse because I couldn't tell if there is a typo somewhere of text is precisely correct and needs to be interpreted as such.
Assuming the text is correct, perhaps it would help if the first sentence of the second paragraph would say something like:
and later on in the same paragraph, something like
Or perhaps someone can come up with a much more clear wording than I can :) https://stackoverflow.com/a/36585703/1517753 ?
Thank you for this documentation!