Promises
Using Java methods with TypeScript's async support.
JavaScript (and thus TypeScript) is built on top of the idea of the event loop. Modern JavaScript has abstracted these asynchronous tasks into Promises
, which can be nicely used with the await
feature.
Unfortunately, Java doesn't have such nice support for asynchronous operations. There are two primary ways APIs use asychronous support in Java. T
CompleteableFuture
The modern way is using the Java CompleteableFuture
class.
As the CompleteableFuture
class has similar semantics to JavaScript promises, Evo's Java API implicitly converts them to Promises for you.
Callbacks
The other way is using callbacks.
Lambda Methods
One way that callbacks are handled in Java is by constructing an interface with only one method to override (i.e. a lambda interface), and running an async method on that which runs the method once it's ready.
Using the promises
API though, this becomes much nicer.
Lambda Arguments
The other way is to pass in a lambda to some other method that handles async behavior.
Fortunately, the promises
API works for this too. It will use the first lambda interface argument (i.e. BukkitRunnable
) as what to listen for when resolving the promise.
Last updated