Race condition in JavaScript with compound assignment

后端 未结 2 996
礼貌的吻别
礼貌的吻别 2021-02-20 13:38

I\'m not talking about complex race conditions involving the network or events. Rather, I seem to have found out that the += operator is not atomic in V8 (Chrome 58

2条回答
  •  离开以前
    2021-02-20 13:54

    This is not a race condition, because you are explicitly yielding the execution using await.

    The standard defines that a compound assignment such as += is not atomic: The left-hand-side of a compound assignment is evaluated before the right-hand-side.[1]

    So if your RHS changes acc somehow, the changes will be overwritten. Most simple example:

    var n = 1;
    n += (function () {
        n = 2;
        return 0;
    })();
    
    console.log(n);

提交回复
热议问题