Await 的运算符优先级是什么? [英] What is the Operator Precedence of Await?
问题描述
在 Javascript 中,某些运算符先于其他运算符处理:
In Javascript certain operators are processed before others:
1 + 2 * 3
// 1 + (2 * 3)
// 7 because * has higher precedence than +
1 === 0 + 1
// 1 === (0 + 1)
// true because + has a higher precedence than ===
MDN 有一个所有运营商的完整分类 和它们的优先级……除了 await
.
The MDN has a full breakdown of all operators and their precedence ... except await
.
await getFoo() * 2; // await (getFoo() * 2) or (await getFoo()) * 2?
await getFoo() === 5; // await (getFoo() === 5) or (await getFoo()) === 5?
谁能解释一下在 await 之前/之后处理了哪些运算符?
Can anyone explain which operators are processed before/after await?
现在我觉得我必须添加一堆可能是不必要的括号,因为我不确定在 await
之前/之后会处理什么.虽然我知道我应该能够查到这个,但即使是 MDN(恕我直言,文档的黄金标准)也没有答案.
Right now I feel like I have to add a bunch of parenthesis that are probably unnecessary because I'm not sure what will get handled before/after await
. And while I know I should just be able to look this up, even MDN (the gold standard of documentation IMHO) doesn't have the answer.
推荐答案
An AwaitExpression 是一个 UnaryExpression 和 delete
、void
、typeof
、+
具有相同的优先级,-
、~
和 !
,绑定比任何二元运算符都强.
An AwaitExpression is a UnaryExpression and has the same precedence as delete
, void
, typeof
, +
, -
, ~
, and !
, binding stronger than any binary operator.
这与 yield
不同,后者的优先级低于除逗号运算符之外的任何其他内容.做出这个设计决策是因为两者都yield a+b
和 await a + await b
是被认为比 (yield a) + (yield b)
和 await (a + b)
更常见的场景代码>.
This is unlike yield
which has a precedence lower than anything else except the comma operator. This design decision was made because both yield a+b
and await a + await b
are scenarios thought to be more common than (yield a) + (yield b)
and await (a + b)
.
这篇关于Await 的运算符优先级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!