0

First of all, it is obvious that I don't understand the thenApply well and that's why I am coming with a compiler error, but I tried to search and couldn't.

Here is my simple code:

import java.util.concurrent.CompletionStage;

public class Main5 {
    public static void main(String[] args) {

    }

    static class Game {
        public void doIt(int id) {
            CompletionStage<Player> player = getPlayer(id).thenApplyAsync(p -> {
                modifyPlayer(p.getId());
                return getPlayer(p.getId());
            });
        }

        private CompletionStage<Player> getPlayer(int id) {
            //do http request to get the player info
        }

        private CompletionStage<Void> modifyPlayer(int id) {
            //do http request to modify player's info
        }
    }
}

In the thenApplySync I am getting this compiler error:

incompatible types: inference variable U has incompatible bounds
    equality constraints: com.testapp.Player
    lower bounds: java.util.concurrent.CompletionStage<com.testapp.Player>

The getPlayer method returns a completion stage already, so what's wrong with that?

I know that

if I do: return getPlayer(p.getId()).toCompletableFuture().get(); it will work but I don't understand why. In my mind, i should return completion stage, not the object.

Help is appreciated.

Jack Thomson
  • 193
  • 8
  • 2
    You should use `thenComposeAsync`. – Johannes Kuhn Nov 10 '19 at 14:27
  • You should return the Player from inside the thenApplyAsync. If you return the CompletionStage from thenApplyAsync then it's another completable task which need to execute and that's why to fix it you have to call get() on it. If you want to combine multiple Completable tasks then you can check this link: https://www.thetechnojournals.com/2019/10/how-to-read-large-file-in-java.html – Ashok Prajapati Nov 10 '19 at 15:28

0 Answers0