Skip to content
Advertisement

toUpperCaseFunction does not work with await keyword

I was trying to use an if clause like this:

JavaScript

I got an error:

TypeError: database.getValue(…).toUpperCase is not a function

However when I change the above if clause to

JavaScript

It works fine.

Why does this happen in Nodejs?

Advertisement

Answer

Property access (.) has higher operator precedence (20) than await (17). Your current code is equivalent to:

JavaScript

because the .s get chained together before the await tries to resolve the expression on the right – which doesn’t work because the getValue call returns the Promise.

You could also fix it by grouping the await getValue call in parentheses:

JavaScript

Or by using a regular expression to get around the == operator precedence (see revision history, I wouldn’t recommend it, it’s very weird).

Personally, I’d prefer your original code of extracting it into a variable first, it’s a bit more readable

Advertisement