nus-cs2030 / 2223-s1

MIT License
1 stars 0 forks source link

thenCombine vs thenCompose #347

Open cherylchan02 opened 1 year ago

cherylchan02 commented 1 year ago

Both of these methods are used to combine 2 completable futures asynchronously, but I'm not very sure what differentiates them and which method to use in what circumstance. Would appreciate if someone could explain this thanks!

valerietanhx commented 1 year ago

I believe thenCompose is used if you want to compose two Futures, so the output of one becomes the input of the other. On the other hand, thenCombine is used if you want to put together both the outputs of each Future.

e.g. bar(foo(a)) --> thenCompose, but bar(a) + foo(a) --> thenCombine

CMIIW!

sumhungyee commented 1 year ago

thenCombine is similar to thenApply but uses a Bifunction instead. thenCompose is similar to flatmap, thenApply is map

mohddanesh commented 1 year ago

agree with @sumhungyee. comparing it to map will make it easier to understand