Menu Close

From Java to JavaScript – Callback and Promise

Java and JavaScript have some things in common, but also do most of them differently. One of them is the way of handling asynchronous side effect or basically operations that may take a while before you can expect a result. These operations usually block the executing program, which means they have to be handled in a special way. Otherwise, the user might have a bad experience, especially if graphical elements are involved which start to get laggy.

Callbacks and asynchronous code in Java

In Java, you have access to multiple threads in your program. So you can shift code that is computationally high in load to a new thread. Threads share a common resource (like a part of memory), and they can run simultaneously. This may improve the effectiveness of your code. Usually everything you do that takes a time (like client/server communication) is done in a separate thread. The main thread or UI thread is the one that should not have blocking operations on.

Modern Java Web Frameworks usually have built-in techniques to handle multithreading anyway. Doing it by-hand is often considered to be bad practice, since it is easy to break something. This means that there is something like a thread pool in your application server and you put an annotation like @asynchronous to the method.

Synchronous Java Example

  1. interface ISomeEventListener {
  2.   void someEvent();
  3. }
  4.  
  5. class SynchronousExample {
  6.   private ISomeEventListener someEventListener;
  7.  
  8.   public void setSomeEventListener(ISomeEventListener someEventListener) {
  9.     this.someEventListener = someEventListener;
  10.   }
  11.  
  12.   public void doSomeHeavyCalculation(final long howLongInMs) {
  13.     System.out.println("Doing some heavy work..."); 
  14.  
  15.     // some heavy work...
  16.     try {
  17.       Thread.sleep(howLongInMs);
  18.     } catch (InterruptedException e) {
  19.       e.printStackTrace();
  20.     }
  21.  
  22.     if (this.someEventListener != null) {
  23.       someEventListener.someEvent();
  24.     }
  25.   }
  26.  
  27.   public static void main(String[] args) {
  28.     final SynchronousExample example = new SynchronousExample();
  29.  
  30.     example.setSomeEventListener(() -> {
  31.       System.out.println("Received some event!");
  32.     });
  33.  
  34.     example.doSomeHeavyCalculation(1000);
  35.   }
  36. }

The synchronous example demonstrates the use of event listeners which is part of the observer pattern. At some point (line 27-29), we register an event listener, that will be notified from another point in our program (line 23). The operations are synchronous, meaning that everything will run on the main thread.

Please note that the Java examples make use of lambda expressions, which require at least the Java Development Kit (JDK) 8. You can see one lambda expression in line 27-29. In short, it gives you the opportunity to write short, concise one-method interface code.

The alternative to lambda would be to create an object that implements the ISomeEventListener interface. With a lambda expression, this looks way better in my opinion:

  1. example.setSomeEventListener(new ISomeEventListener() {
  2.       @Override
  3.       public void someEvent() {
  4.         System.out.println("Received some event!");
  5.       }
  6. });

Asynchronous Java Example

  1. class AsynchronousExample {
  2.   private ISomeEventListener someEventListener;
  3.  
  4.   public void setSomeEventListener(ISomeEventListener someEventListener) {
  5.     this.someEventListener = someEventListener;
  6.   }
  7.  
  8.   public void doSomeHeavyCalculation(final long howLongInMs) {
  9.     // some heavy work...
  10.     new Thread(() -> {
  11.       try {
  12.         System.out.println("Doing some heavy work..."); 
  13.         Thread.sleep(howLongInMs);
  14.       } catch (InterruptedException e) {
  15.         e.printStackTrace();
  16.       }
  17.  
  18.       if (this.someEventListener != null) {
  19.         someEventListener.someEvent();
  20.       }
  21.     }).start();
  22.   }
  23.  
  24.   public static void main(String[] args) {
  25.     final AsynchronousExample example = new AsynchronousExample();
  26.  
  27.     example.setSomeEventListener(() -> {
  28.       System.out.println("Received some event!");
  29.     });
  30.  
  31.     example.doSomeHeavyCalculation(1000);
  32.   }
  33. }

The asynchronous example demonstrates the use of event listeners, too. Line 10 shows the creation of a new thread including lambda expressions (the object has to implement the Runnable interface. These operations are non-blocking, meaning that the main thread will not wait for the operation to complete.

Callbacks and asynchronous code in JavaScript

In JavaScript, there is no multithreaded access. One of the reason why is that the language started to be used in the web browser only, and was intended for small, quick running code. Computationally intensive code was intended to run on a remote server. However, there are things like Webworkers which allow you to run code besides independently of your UI stuff. But Webworkers are more a multi-process than multi-thread thing, because they do not really share a (heap) memory or in other words shared data objects.

Callbacks in JavaScript can have a really great use. Listeners are a good example for that. You could pass an innerMethod() to an outerMethod() so that every time the outerMethod() has a result or something to notify, it can invoke the innerMethod(). Probably the most famous example in JavaScript is setTimeout(code, msTime). This will execute code after a given time in milliseconds has passed.

Same applies to setInterval(code, intervalTimeMs), which periodically invokes code. Be sure to clear the timer if not used anymore:

  1. const id = setInterval(() => console.log('do something'), 5000);
  2. clearInterval(id); //...when finished

Have a look at the following example that demonstrates the use of async / await and thenable code, which has the same semantics but different syntax:

  1. const fetch = require("node-fetch"); // npm install node-fetch
  2.  
  3. const getSomeData = async () => {
  4.   try {
  5.     const response = await fetch('https://swapi.co/api/people/?search=r2');
  6.  
  7.     if (response.ok) {
  8.       return await response.json();
  9.     } else {
  10.       throw response.status;
  11.     }
  12.   } catch (error) {
  13.     throw error;
  14.   }
  15. }
  16.  
  17. getSomeData()
  18.   .then(json => console.log(json.results[0].name))
  19.   .catch(error => console.error(error))

As you can see, the code fetches some data from the Star Wars API. The important thing to notice here is that line 5 does more-or-less the same as line 17/18. The keyword await simply means that it waits (pauses the execution) for the Promise to resolve (fulfill, it could also fail / throw) and returns its thenable value. Async / await has been included in the ES6 (ECMA Script 2015).

Leave a Reply

Your email address will not be published. Required fields are marked *

nine + nineteen =

This site uses Akismet to reduce spam. Learn how your comment data is processed.