spotify / completable-futures

Utilities for working with futures in Java 8
Apache License 2.0
393 stars 51 forks source link

Supply compose async #97

Closed thiml closed 10 months ago

thiml commented 10 months ago

Context

Adds a utility function supplyAsyncCompose which removes the need for dereferencing the output of supplyAsync whenever the Supplier return type is a CompletionStage<T> to avoid bloating of unwrapping the nested CompletionStage<CompletionStage<T>>.

This is beneficial when one would like to create Future chains on top of the Supplied type:

Example

// Before
// databaseOperation returns a CompletionStage
CompletableFuture.supplyAsync(() -> databaseOperation().thenApply(DataBaseRow::toProto)).thenCompose(result -> result);
// After
// databaseOperation returns a CompletionStage
CompletableFuture.supplyAsyncCompose(() -> databaseOperation().thenApply(DataBaseRow::toProto));

NOTES This might be achievable by wrapping every supplyAsync with the already existing dereference to get the same behaviour. This might be preferable, but if this seems valuable feel free to review and merge.