简体   繁体   中英

Cannot call javascript's async functions from nashorn java

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?

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 Promise s, or maybe compile it down to ES5 with something like babel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM