Get JavaScript result on to the ServerSide-Vaadin 14

Hello! I have the following function in my js file :

window.msg=function(name){
	alert("Hello from script ->"+name);  
	return name; 
}

I call the msg function on the server side using this:

String param="someMessage"; 
String command="msg("+'"'+param+'"'+")"; 
PendingJavaScriptResult pendingJavaScriptResult1=page.executeJs(command);

How can I get the result of the method “msg” on the server side?
Thanks!

Use then method of the PendingJavaScriptResult instance.
It accepts consumers which you should provide to deal with the result or an error.
The consumers will be called once the result is sent from the client to the server side ( asynchronously ).

Denis Anisimov:
Use then method of the PendingJavaScriptResult instance.
It accepts consumers which you should provide to deal with the result or an error.
The consumers will be called once the result is sent from the client to the server side ( asynchronously ).
Thanks for your response. Can you give me an exemple, please? I try to use “then” method but I got this error “incompatible types found void”.

String command="msg("+'"'+param+'"'+")";

the command doesn’t return anything.
So it’s “return type” is void . That’s the reason why you get the error.
You shout return something in your JS : "return msg("+'"'+param+'"'+")" e.g.

Denis Anisimov:
String command="msg("+'"'+param+'"'+")";

the command doesn’t return anything.
So it’s “return type” is void . That’s the reason why you get the error.
You shout return something in your JS : "return msg("+'"'+param+'"'+")" e.g.

I have return statement in msg function ( in js file ). If I write return in the command, the function msg is called over and over again similar to a while loop. It doesn`t work.

I don’t think I understand you .

When you call executeJs you should have an arg which has a return statement.
So it should be page.executeJs("return 'a';"); e.g.
Pleases try with this simplest script, check that it works and then use your function.

Denis Anisimov:
I don’t think I understand you .

When you call executeJs you should have an arg which has a return statement.
So it should be page.executeJs("return 'a';"); e.g.
Pleases try with this simplest script, check that it works and then use your function.

I try this simple exemple and works fine. But what I want to do is to get the “name” which is returned in msg function in js file. On the server side page.executejs(command) calls the method from javascript. e.g : command = “msg(‘sometext’)”;

page.executeJs(command).then(String.class, result -> {
            System.out.println("type = "+result.getClass());
            System.out.println("result is -> "+result.toString());
        });

As I said: you should invoke it via page.executeJs("return window.msg('"+param+"');"); or
page.executeJs("return window.msg($0);", param);

Denis Anisimov:
As I said: you should invoke it via page.executeJs("return window.msg('"+param+"');"); or
page.executeJs("return window.msg($0);", param);

I try this but everytime I get “false” as a result instead of the string name which was sent as a parameter. I dont understand what Im doing wrong.

It means that the function which you call returns true.

It means that you either have another definition of the function msg which overrides window.msg or you are calling another function.

Make another function with a different name and try it.
Use console.log() inside you Js function to make sure that you are calling the correct function .
Use console.log( msg('whatever')) to make sure that the result is what you expect.

Another way to achieve the same is
Element::executeJs("this.$server.someFunction($0), param); for Component (with the appropriate element) where someFunction is declared as @ClientDelegate in Java.

Denis Anisimov:
It means that the function which you call returns true.

It means that you either have another definition of the function msg which overrides window.msg or you are calling another function.

Make another function with a different name and try it.
Use console.log() inside you Js function to make sure that you are calling the correct function .
Use console.log( msg('whatever')) to make sure that the result is what you expect.

Another way to achieve the same is
Element::executeJs("this.$server.someFunction($0), param); for Component (with the appropriate element) where someFunction is declared as @ClientDelegate in Java.

Thanks!