executeJS returning elemental.json.impl.JreJsonNull

Hi,

I’m trying to execute a JavaScript function returning a value with executeJS. Console.log shows that it returns a value but my callback on the Java side indicates a JreJsonNull. Any pointers?

Can you give a code sample?

-Olli

function foo() {
	console.log("<<< called >>>");
	return "bar";
}

and

		PendingJavaScriptResult scriptResult = UI.getCurrent().getPage().executeJs("foo()");
		scriptResult.then(response -> {
			Notification.show(response.asString());
		});

Although the JavaDocs are a bit confusing:

Adds an untyped handler that will be run for a successful exception. The handler will be invoked asynchronously if the execution was successful. In case of a failure, no handler will be run.

Have you tried .executeJs("return foo()"); ? Looks like that particular executeJs call isn’t returning any value.

Ah, of course. Thanks, that did the trick. Also shows my level of JavaScript-knowledge.

Don’t worry, it’s not really obvious - it took me a while to figure out too.

Olli Tietäväinen:
Don’t worry, it’s not really obvious - it took me a while to figure out too.

Hi do you have idea about how to pass promise resolved value to java from js?

a js file

window.call = ()=>{
	api.then(result => {
		// how can I get this from Java file?
		console.log(result.data)
	}).catch(error => {
		console.warn(error)
	})
}

a java file

private void method(){
	getElement().executeJs("return call()")
				// getting elemental.json.impl.JreJsonNull here
				.then(item -> Notificiation.show(item.toString()))
}

many thanks!!