{"action":"created","issue":{"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/399","labels_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/399/labels{/name}","comments_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/399/comments","events_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/399/events","html_url":"https://github.com/AsyncHttpClient/async-http-client/issues/399","id":20661798,"number":399,"title":"Support for Java 8 CompletableFuture<T> as suppliment to Future+CompletionHandler","user":{"login":"tnn","id":161150,"avatar_url":"https://avatars.githubusercontent.com/u/161150?v=3","gravatar_id":"","url":"https://api.github.com/users/tnn","html_url":"https://github.com/tnn","followers_url":"https://api.github.com/users/tnn/followers","following_url":"https://api.github.com/users/tnn/following{/other_user}","gists_url":"https://api.github.com/users/tnn/gists{/gist_id}","starred_url":"https://api.github.com/users/tnn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tnn/subscriptions","organizations_url":"https://api.github.com/users/tnn/orgs","repos_url":"https://api.github.com/users/tnn/repos","events_url":"https://api.github.com/users/tnn/events{/privacy}","received_events_url":"https://api.github.com/users/tnn/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/labels/Enhancement","name":"Enhancement","color":"e102d8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2013-10-08T06:54:43Z","updated_at":"2015-02-05T08:14:46Z","closed_at":null,"body":"JDK 8 introduces a composable standard Future called CompletableFuture[1]. Even through it will be a pain to support this while retaining backwards compatibility, adding support for this standard future is very important when designing complete async systems. It enables the same returned Future to be reused, composed[2] and simplifies the usage of AsyncHttpClient.\r\n\r\nExample usage:\r\n```\r\nasyncHttpClient.prepareGet(url)\r\n    .exceuteJava8Style()\r\n    .thenApply(res -> doStuff(res))\r\n    .exceptionally(e -> log.error(\"Got exception\", e));\r\n```\r\n\r\n[1] http://download.java.net/jdk8/docs/api/java/util/concurrent/CompletableFuture.html \r\n[2] http://nurkiewicz.blogspot.dk/2013/05/java-8-completablefuture-in-action.html"},"comment":{"url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/comments/73009529","html_url":"https://github.com/AsyncHttpClient/async-http-client/issues/399#issuecomment-73009529","issue_url":"https://api.github.com/repos/AsyncHttpClient/async-http-client/issues/399","id":73009529,"user":{"login":"alinpopa","id":7199,"avatar_url":"https://avatars.githubusercontent.com/u/7199?v=3","gravatar_id":"","url":"https://api.github.com/users/alinpopa","html_url":"https://github.com/alinpopa","followers_url":"https://api.github.com/users/alinpopa/followers","following_url":"https://api.github.com/users/alinpopa/following{/other_user}","gists_url":"https://api.github.com/users/alinpopa/gists{/gist_id}","starred_url":"https://api.github.com/users/alinpopa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alinpopa/subscriptions","organizations_url":"https://api.github.com/users/alinpopa/orgs","repos_url":"https://api.github.com/users/alinpopa/repos","events_url":"https://api.github.com/users/alinpopa/events{/privacy}","received_events_url":"https://api.github.com/users/alinpopa/received_events","type":"User","site_admin":false},"created_at":"2015-02-05T08:14:46Z","updated_at":"2015-02-05T08:14:46Z","body":"What about using a `CompletableFuture` as a `Promise`, something like this:\r\n```\r\npublic <T> CompletableFuture<T> get(String url, Function<Response, T> supplier){\r\n        final CompletableFuture<T> toBeCompleted = new CompletableFuture<>();\r\n        asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<Response>() {\r\n            @Override\r\n            public Response onCompleted(Response response) throws Exception {\r\n                toBeCompleted.complete(supplier.apply(response));\r\n                return response;\r\n            }\r\n\r\n            @Override\r\n            public void onThrowable(Throwable t){\r\n                toBeCompleted.completeExceptionally(t);\r\n            }\r\n        });\r\n        return toBeCompleted;\r\n}\r\n\r\nfinal Function<Response, String> responseProducer = (response) -> {\r\n    try {\r\n        return response.getResponseBody();\r\n    } catch (Exception e) {\r\n        return e.getMessage();\r\n    }\r\n};\r\n\r\nfinal CompletableFuture<String> f =\r\n    get(\"http://127.0.0.1:8080/bla1\", responseProducer)\r\n        .thenCompose(response -> get(\"http://127.0.0.1:8080/\" + response, responseProducer);\r\n```\r\n\r\nThat's the alternative that I found ... not sure if it's the best, what do you think?"}}