Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active April 4, 2018 06:52
Show Gist options
  • Select an option

  • Save adamw/affa7d89576e9c9723d3d900f517deb7 to your computer and use it in GitHub Desktop.

Select an option

Save adamw/affa7d89576e9c9723d3d900f517deb7 to your computer and use it in GitHub Desktop.
class Wrappers {
// I/O operations: non-blocking, asynchronous
<T> CompletableFuture<T> fetchFromDb(Class<T> entityClass, long id) { return null; }
CompletableFuture<String> sendHttpGet(String url) { return null; }
CompletableFuture<Void> sendEmail(String to, String content) { return null; }
// the bussines logic: asynchronous
CompletableFuture<Void> runBusinessProcess() {
// thenCompose: also known as flatMap, combines two futures
return fetchFromDb(User.class, 42).thenCompose(user -> {
// storing the intermediate result in variables
CompletableFuture<String> httpResult =
sendHttpGet("http://profile_service/get/" + user.getProfileId());
return httpResult.thenCompose(profile ->
sendEmail(user.getEmail(), "Your profile is: " + profile));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment