Skip to content

Commit ee5fe8f

Browse files
committed
[BAEL-9461] - Added code examples for difference between thenApply() and thenCompose()
1 parent 5a76e44 commit ee5fe8f

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

core-java-concurrency/src/test/java/com/baeldung/completablefuture/CompletableFutureLongRunningUnitTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,25 @@ public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputatio
184184

185185
assertEquals("Hello World", future.get());
186186
}
187+
188+
@Test
189+
public void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException {
190+
CompletableFuture<Integer> finalResult = compute().thenApply(s -> s + 1);
191+
assertTrue(finalResult.get() == 11);
192+
}
193+
194+
@Test
195+
public void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException {
196+
CompletableFuture<Integer> finalResult = compute().thenCompose(this::computeAnother);
197+
assertTrue(finalResult.get() == 20);
198+
}
199+
200+
public CompletableFuture<Integer> compute(){
201+
return CompletableFuture.supplyAsync(() -> 10);
202+
}
203+
204+
public CompletableFuture<Integer> computeAnother(Integer i){
205+
return CompletableFuture.supplyAsync(() -> 10 + i);
206+
}
187207

188208
}

logging-modules/log4j2/${sys:logging.folder.path}

Whitespace-only changes.

0 commit comments

Comments
 (0)