操作员等待的优先顺序是什么? [英] 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?
任何人都可以解释在等待之前/之后要处理哪些运算符吗?
Can anyone explain which operators are processed before/after await?
现在,我觉得我必须添加一些可能不需要的括号,因为我不确定在await
之前/之后将要处理的内容.虽然我知道我应该可以查找一下,但是即使MDN(文档IMHO的黄金标准)也没有答案.
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.
推荐答案
一个 AwaitExpression 是 UnaryExpression 并具有与delete
,void
,typeof
,+
,-
,~
和!
相同的优先级,其绑定性强于任何二进制运算符.
An AwaitExpression is an 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)
.
这篇关于操作员等待的优先顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!