Search code examples
javascriptsyntaxsafariasync-awaitoperator-precedence

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();
}

Solution

  • 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.