PendingJavaScriptResult::toCompletableFuture does not terminate

Hello everyone,

I am trying to pass the value of my [monaco editor]
( via javascript from the browser to my backend:

getElement().executeJs("return editor.getValue()").then(jsonValue -> System.out.println(jsonValue.asString()));

The above code snipped functions correctly. I can get the value in an asynchronous way. The use case demands that I get the value in a synchronous way. So I decided to use the ::toCompletableFuture method:

getElement().executeJs("return editor.getValue()").toCompletableFuture().get().asString();

Which results in an endlessly waiting application, because the CompletableFuture does not terminate. It’s easily reproducible with this javascript snippet:

getElement().executeJs("return \"Text added 123\"").toCompletableFuture().get().asString();

Am I using it the wrong way or is this behaviour buggy?

Thanks in advance,

CompletableFuture.get() will block the thread until the value is available. This will also block Vaadin from sending the JS to the client side as this happens at the end of the request, when all your event handlers have been run. Thus the thread will wait for the response and Vaadin will wait for your method to finish before executing the JavaScript.

There is unfortunately no way to do that in a synchronous way. However, you can use Kotlin and Coroutines to make the code look synchronous (but still work asynchronously under the hood).

Thanks. I guess I’ll need to build a workaround.