Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should API return CompletionStage or CompletableFuture

When building an API it is a good practice to code to an interface so it seems like returning CompletionStage seems like a best approach. However I realized that I happen to be always calling .toCompletableFuture after getting the CompletionStage. What is the recommended approach in this case?

like image 238
user_1357 Avatar asked Mar 10 '23 01:03

user_1357


1 Answers

See my reply at https://stackoverflow.com/a/49158702/1113842

Like Holger asked, why do you call toCompletableFuture?

  • Do you want to the caller to complete/cancel it? Most applications provide their own mechanisms for completing the stage. Thus, it's not necessary to expose these methods.
  • Do you want to use the blocking get? This should be avoided in async programming.

If the answers are no to both questions, you should return CompletionStage.

like image 191
Xiao Avatar answered Mar 15 '23 07:03

Xiao