我知道 JavaScript 是单线程的,从技术上讲,它可以\xe2\x80\x99t 具有竞争条件,但由于异步和事件循环,它可能具有一些不确定性。这里\xe2\x80\x99s是一个过于简单的例子:
\nclass TestClass {\n // ...\n\n async a(returnsValue) {\n this.value = await returnsValue()\n }\n b() {\n this.value.mutatingMethod()\n return this.value\n }\n async c(val) {\n await this.a(val)\n // do more stuff\n await otherFunction(this.b())\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n假设b()依赖于this.value自调用 以来没有发生更改a(),并且c(val)从程序中的多个不同位置快速连续地多次调用。这是否会造成数据竞争,导致和 的this.value调用之间发生变化?a()b()
作为参考,我已经使用mutex预先解决了我的问题,但我\xe2\x80\x99一直在质疑是否存在问题。
\n