Last active
April 4, 2018 06:52
-
-
Save adamw/affa7d89576e9c9723d3d900f517deb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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