Java versus C# asynchronous abstractions
Do you know the difference between the behavior of the two code snippets that answer the question “Java Equivalent of C# async/await?”?
With almost 100K views on Stackoverflow, many developers are still looking for
alternatives to Java absence of the well-known async
/await
idiom found in most
programming languages such as Js, C#, Kotlin or Python.
Almost two years after the original post, the CCISEL engineer Miguel Gamboa has
presented a Java non-blocking alternative to get the body size of an HTTP
request response, which is still attracting increasing attention.
Nevertheless, although concise and non-blocking this solution still presents a
subtle different behavior from C# async/await use case of the original post.
Can you guess what is it?
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
var urlContents = await client.GetStringAsync("http://msdn.microsoft.com");
return urlContents.Length;
}
CompletableFuture<Integer> AccessTheWebAsync()
{
return asyncHttpClient()
.prepareGet("http://msdn.microsoft.com")
.execute()
.toCompletableFuture()
.thenApply(Response::getResponseBody)
.thenApply(String::length);
}