I am aware JavaScript is single-threaded and technically can’t have race conditions, but it supposedly can have some uncertainty because of async and the event loop. Here’s an oversimplified example:
class TestClass {
// ...
async a(returnsValue) {
this.value = await returnsValue()
}
b() {
this.value.mutatingMethod()
return this.value
}
async c(val) {
await this.a(val)
// do more stuff
await otherFunction(this.b())
}
}
Assume that b()
relies on this.value
not having been changed since the call to a()
, and c(val)
is being called many times in quick succession from multiple different places in the program. Could this create a data race where this.value
changes between calls to a()
and b()
?
For reference, I have preemptively fixed my issue with a mutex, but I’ve been questioning whether there was an issue to begin with.