Diwahar
Diwa's Blog

Diwa's Blog

Completable Future: The Promises of Java

Completable Future: The Promises of Java

Diwahar's photo
Diwahar
·Apr 4, 2022·

3 min read

Introduction

People who have been exposed to languages like C# or Javascript would have already figured out what I'm going to talk about in this blog. For those who have just been coding in Java right from the start of your career, or who have just started their career in a language other than javascript, this blog is for you.

The Interview

During an interview with one of the famous banks in the US, there was a question from the interviewer asking me to optimize a solution which was making 5 API calls sequentially with each call taking more than 200 ms to respond. As a java developer, I was very well able to identify the bottlenecks and I proposed to solution to make all the API calls parallel using the asynchronous approach with the help of ExecutorServices. The interviewer wasn't much convinced with the answer though the proposed solution would have still solved the problem.

The Optimized Solution

One month went by and I started to learn Javascript as part of my new job(from a different company) and then I came across the concepts of callbacks and promises in javascript. I watched a lot of tutorials on Youtube and went through each and every line of MDN documentation. I was now able to understand what the interviewer(from the US bank) was expecting and where things went wrong during the interview.

Promises in Javascript

Javascript has a concept called 'callbacks', wherein a task is instructed to run asynchronously and perform an operation once the task is completed(or failed) successfully. People started to chain a lot of callbacks at once and over a period of time, this became callback hell as the syntaxes we difficult to read by other programmers. The ECMA standard of javascript added a beautiful syntactic sugar on top of callbacks and came up with the new concept called Promises.

The promise would accept two functions: resolve & reject namely and in case of successful completion, the resolve function gets called and reject when there is an error. A series of promises can all be chained to using the 'then' keyword and in situations where there are exceptions, we can have a catch block just like how you would handle exceptions. The basic syntax of promises is given below.

carbon (2).png

Completable Future

Once I learned the effectiveness of promises in javascript, I was wondering if I could find a similar solution in java and finally ended up with CompletableFutures. CompletableFuture was first introduced in java 8 to overcome the shortcomings of Future in java which was blocking when an asynchronous task was taking more time to complete its work. Completable future just like promises in java would return the result of the task upon completion and triggers the callback methods.

CF_java.png

However, in order to understand completable futures fully, one should have a basic understanding of Functional Interfaces and lambda expressions in java.

Summary

If you are already aware of the promises in javascript, then Completable Futures are its equivalent in java. So, next time when you want to perform something in java asynchronously and associate a call back with it, go for CompletableFutures.

 
Share this