Menu

1 / 3

No Threads, Still Races: Two awaits Can Corrupt State a Java Lock Would Have Guarded

DEV Community: typescript·Gabriel Anhaia·10 days ago
#LmqbCEws
#devtotypescript#devto#dev#tail#typescript
Reading 0:00
15s threshold

Book: Kotlin and Java to TypeScript

Also by me: The TypeScript Library — the 5-book collection

My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools

Me: xgabriel.com | GitHub

An account holds 100. Two requests arrive at the same moment, each asking to withdraw 80. Your TypeScript service approves both, pays out 160, and records a final balance of 20.

If you came from a JVM codebase, you would have wrapped that method in synchronized without thinking. Then someone told you Node runs your JavaScript on a single thread, and the lock instinct went quiet. The single thread is real. The safety you inferred from it is not, and the gap sits at every await in your code.

Where "single-threaded" stops protecting you

The Node.js docs say it plainly: the event loop gives Node non-blocking I/O "despite the fact that a single JavaScript thread is used by default" .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.