I want to get a value inside a CompletableFuture (in this case clonedWorld) in another CompletableFuture and return that future. This is my code, and I'm stuck here:
@Override
public CompletableFuture<SlimeWorld> asyncCloneWorld(String worldName, String newWorld) {
loadWorldAsync(worldName).whenComplete((slimeWorld, throwable) -> {
if (throwable != null || slimeWorld.isEmpty()) {
plugin.getLogger().log(Level.SEVERE, "Impossibile caricare il mondo template!", throwable);
return;
}
try {
SlimeWorld clonedWorld = slimeWorld.get().clone(newWorld, loader, true);
} catch (IOException | WorldAlreadyExistsException e) {
plugin.getLogger().log(Level.SEVERE, "Non è stato possibile clonare il mondo: " + worldName, e);
}
});
return ?;
}
Your problem is that
whenComplete()only takes aBiConsumeras parameter, which cannot handle the result of the returnedCompletableFuture– except if an exception is thrown.The appropriate method to use if you want to change the result (here from something like
Optional<SlimeWorld>or an exception to justSlimeWorldornull) ishandle(). The lambda passed tohandle()should thus return the final result (the cloned world ornull).And as
CompletableFutureis a fluent API, you can just return the result of thehandle()call fromasyncCloneWorld():