If you are a full stack Java developer, there is a good chance that you have met promises in JavaScript.
Promises are simple abstractions that don't impose strict requirements on you; you can use them to calculate the result on some other thread, light process, or anything you like.
In Java, there are a couple of ways to achieve this; one of them is with futures (java.util.concurrentFuture
) and if you want something more similar to JavaScript's promise there is a nice implementation called
jdeferred (https://github.com/jdeferred/jdeferred), which you might have used before.
In essence a promise is just a promise that you can give to your caller, the caller can use it at any given time. There are two possibilities:
If the promise has already been fulfilled, the call returns immediately
If not, the caller will block until the promise is fulfilled
Let's take a look at an example; remember to use the start-thread function in the clojure-concurrency.core
package: