Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator precedence: ! and await

In JavaScript, how is the following statement to be interpreted:

cond1 && !await f()

This is an excerpt from the line

if(cond1 && !await f()){
    do_stuff();
}

inside a production application. chrome seems to be fine with it, but on ios this causes an error reading

unexpected identifier 'f'. Expected ')' to end an if condition.

It looks as if ios turns !await f() into (!await)(f()) instead of !(await f()).

Now to my question: According to ECMA-262 what is the correct interpretation of the above line?

p.s.: We have fixed the code for ios by changing it to

var f_result = await f();
if(cond1 && !f_result){
    do_stuff();
}
like image 485
niklasfi Avatar asked Oct 17 '22 09:10

niklasfi


1 Answers

This has nothing to do with operator precedence. Since both are unary prefix operators, there's only a single way this expression can be interpreted - all of delete, void, typeof, +, -, ~, ! and await are parsed with the same production goal and can be arbitrarily nested. And yes, it's valid syntax in ES2017.

like image 162
Bergi Avatar answered Oct 21 '22 05:10

Bergi