News for hackers. Stuff that matters.
Re:How JavaScript works: Event loop and the rise of Async pr... by johnsonjo on Saturday October 14, 2017 @09:10PM (#15475281)
Here's a lock implementation I made just barely as a proof of concept:

https://bl.ocks.org/johnsonjo4531/99256568deaf8c0f1685793a4a...

Basically it is used like this:

```

  var lock = new AsyncLock();

  (async () => {
    for await(var _ of lock) {
      console.log("here 1a");
      await sleep(1000);
      console.log("here 1b")
    }
  })();

  (async () => {
    for await(var _ of lock) {
      console.log("here 2a");
      await sleep(1000);
      console.log("here 2b")
    }
  })();

  /*
  When Lock is working should print:
  here 1a
  here 1b
  here 2a
  here 2b
  Without the lock it would print:
  here 1a
  here 2a
  here 1b
  here 2b 
  */
```

If you want to check out the implementation go to the blocks link. (which is basically just a gist that you can run). Here's the actual gist link: https://gist.github.com/johnsonjo4531/99256568deaf8c0f168579....

It requires Chrome 63 (at this point Chrome Canary) because it uses async iterators. I used this because I can close the lock automatically in case an error is thrown, the loop is broken. It only iterates one value and only after the lock is unlocked.

There are no comments on this comment yet.