Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Releasing a Lock With using

Releasing a Lock With using

A try/finally releases the lock, but the next function that needs a job’s lock would have to write it all again. Priya’s Python doesn’t, because JobLock is a context manager: with calls __enter__() to take the lock and __exit__() to release it.

JavaScript’s counterpart has only the release half. The constructor takes the lock, as __enter__() does, with the lines that took it in runJob(), and a method keyed by Symbol.dispose, a built-in symbol, releases it with the lines that released it.

Python marks its hooks with double underscores; JavaScript uses a symbol, so no ordinary method name can match it. The square brackets make it a computed key. Unlike __exit__(), the method takes no arguments. An object with this method is a disposable:

Language: JavaScript
class JobLock {
  constructor(jobId) {
    this.name = `${jobId}.lock`;
    this.path = join(LOCKS, this.name);
    writeFileSync(this.path, "", { flag: "wx" });
    console.log(`  [lock] took ${this.name}`);
  }

  [Symbol.dispose]() {
    unlinkSync(this.path);
    console.log(`  [lock] released ${this.name}`);
  }
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll hand Priya’s job lock to JavaScript’s using declaration, and find out how long it holds the lock. Your port still takes and releases it by hand.

00:12 The next function that needs a job’s lock would write them all again. In Python, it’s one with line, because JobLock carries its own cleanup.

00:23 Copy your port to runner.js. Keep that file for the rest of the course. Now make JobLock a JavaScript class. Python’s with calls __enter__() to take the lock, and __exit__() to release it.

00:38 JavaScript’s protocol has only the release half. So the lock is taken in the constructor, which runs when new creates the object.

00:48 The lines that took the lock move into the constructor. It keeps both the file name and the path. A JavaScript path is a plain string, so it has no name property.

01:00 writeFileSync() takes the lock with the W X flag, just as touch() does. If the lock file already exists, new throws EEXIST before there’s an object to release, so the other PC’s lock stays safe, as in Python.

01:16 The release goes in a method keyed by Symbol.dispose, a built-in symbol. Python marks its hooks with double underscores; JavaScript uses a symbol, so no ordinary method name can match it.

01:29 That’s all you need about symbols for now: they get a course of their own later. The square brackets make the symbol the key. The lines that released the lock move into its body, which, unlike __exit__(), takes no arguments.

01:45 Such an object is a disposable.

01:49 In runJob(), the lines the class took over, with their try and finally, become one declaration: using lock, set to a new JobLock.

01:59 using declares a constant, like const. When the block around it ends, however it ends, JavaScript calls the lock’s dispose method.

02:10 Watch out: using needs that exact method, so a class with only a close() method throws a TypeError at the using line, and the lock the constructor took is left behind.

02:22 Python’s with is as strict about __exit__(), but it checks before taking anything. Only null and undefined are allowed without one: using skips them.

02:34 In Python, J-311’s lock is released before its done line. Your turn: pause the video and predict which of those two lines JavaScript prints first for J-311.

02:52 The done line. J-311 logs done while it still holds its lock, and only then releases it.

03:02 using has no indented body. Like a const, it lasts until its enclosing block ends, and here, that block is the whole function. Python ties the release to the with statement, so it comes at the end of the indentation.

03:18 So J-311’s lock is held through its done line, and anything slow added there later, such as a report, would keep the other PC out of J-311 the whole time. The fix is a bare block: a pair of braces with no if or loop in front.

03:35 Python has nothing like it, because its indented blocks always belong to a statement. Put the using and what the with covered inside it, and leave the done line after it.

03:48 Now the lock lasts exactly as long as the with did. Run the morning again. J-311 now releases its lock before the done line, and every line matches Python’s.

04:03 The copy’s finally runs first, then the lock’s dispose method, at the block’s closing brace. Two using lines in one block release the second one first, as nested with blocks do.

04:16 However the block ends, the lock goes. When J-313’s copyFileSync() throws, the error leaves the block, and the lock is released before main() logs the failure, as in Python.

04:30 That’s the JavaScript way: give a resource class a [Symbol.dispose]() method, and declare it with using in a block exactly as long as you need it.

04:39 Next up, Turning a @contextmanager Into a Disposable, a text lesson: the spool copy gets its own using line, and the inner try goes.

Become a Member to join the conversation.