linkedin/parseq

difference between withSideEffect and andThen

Closed this issue · 2 comments

can you please describe the difference between these 2 and the use cases in which we rather use one of them over the other?
If there is other option to handle promise value of async task execution please explain if it is more recommended in some cases.

Hi,

In short: you can think of a side effect task as a task whose result is not needed to compute result of it's parent (whether task was successful or has failed is part of result). Here is an example use case:

You have a web service which accepts a PUT request. It's implementation needs to persist the data and update a remote cache. Update of a remote cache is an optional optimization. For performance reasons you want to respond to client request ASAP. Clients needs a guarantee that data was persisted but updating remote cache is not part of the contract and client doesn't care about it. In this situation you would use persist.withSideEffect(x -> updateCache(x)). Response would be sent to the client as soon as persist task completes successfully.
However, if updating a remote cache was part of a contract and the PUT request should fail if remote cache update failed then you would use persist.andThen(x -> updateCache(x)). In this case response would not be sent to the client until updateCache() is completed and the whole request would fail if updateCache() failed.

There are many differences between withSideEffect and andThen (there are two kinds of andThen). All the differences are explained in the javadoc, to enumerate them I would pretty much have to copy-paste javadoc. Let us know if there anything we can do in a javadoc to make it clearer.

+1 can the andThen docs be extended to parity match the withSideEffect docs - they are relatively sparse so it's hard to tell the more fine grained details @jodzga

It's difficult to tell from the wording that the task that called andThen will fail and propagate the failure to the andThen-creating task