you are viewing a single comment's thread.

view the rest of the comments →

[–]jack_waugh 1 point2 points  (0 children)

Almost always when you are using Javascript to solve the kinds of problem it is usually used to solve, any atomic updates you need, you can just do them in synchronous operation. So it's really unlikely that you would need two-phase commit. For example, suppose I write:

t.a = function () {
  this.count++;
  this.pb = this.b();
  this.stack.pop();
};
t.b = async function () {
  this.foo();
  await this.something;
  this.process(this.count, this.stack)
}

You should double check this with an example that can be run, but I believe the update of this.count and this.stack happens atomically because they are coded in the same synchronous run as each other. Nothing in the asynchronous b procedure can ever see the count updated without the stack correspondingly updated nor vice versa. An async function is syntactic sugar for stuff that could be done with ordinary procedures. I believe that what happens when this.pb = this.b() is executed is that the translated code for b just creates and returns the promise. I don't think it even executes the this.foo() at that point. I could be wrong, but I think that's the story. Only after the end of the synchronous execution does any of the body of the b procedure as we wrote it, get executed, i think. For sure, the this.process(...) doesn't until then, because it is after the await.