3

I want to call a javascript function like the following from java using nashorn

async function testSample() {    
    for (var i = 0; i < sample.length; i++) {
        await sample[i]();
    }    
}

So it will execute all the functions in the sample variablel. But I am getting the following error

Expected ; but found function
async function testSample()() {
  ^ in <eval> at line number 8 at column number 6

Is there support for async functions on nashorn?

Any work around to solve this?

2
  • put a ; before async then try Commented Nov 13, 2018 at 8:38
  • 2
    @AyushGupta: That's not going to fix the error. Commented Nov 13, 2018 at 8:39

1 Answer 1

6

async is defined in ECMAScript 2017; Nashorn currently only supports ECMAScript 5.1.

Therefore, you cannot directly call this code from Nashorn. You could try to rewrite it using Promises, or maybe compile it down to ES5 with something like babel.

Sign up to request clarification or add additional context in comments.

4 Comments

So there is no way to call async methods.
@venkat Not from Nashorn, no. You can try to rewrite the code using Promises or maybe transpile it to ES5 with babel.
Can Rhino do this? - @ohlec
@venkat I don't think so. Rhino supports JavaScript 1.7, which is Mozilla's versioning system. I'm not sure what standard exactly it corresponds to, but I'm pretty sure it doesn't support async/await

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.