“It’s single-threaded, you don’t need to worry about race conditions.”
Look, I know what you were told. No threads means no more worrying about concurrency, no more byzantine locking code, no more endless debugging sessions for errors that only happen 2% of the time.
If only it were true. Every time a JavaScript function does some I/O, every time some event happens, some other function can start running. Things happen in unpredictable orders, and we need to make sense of it all.
Sure, we’ve come up with a few tools to help. We’ve got callbacks and Async.js, we’ve got promises, we’ve got Web Workers, event emitters, generators and iterators and soon we’ll have async/await. But which one to pick, and when? All of them have different trade-offs and are useful for different things.
And then there’s all the things we haven’t tried yet, things we could steal from other languages. Mutexes and condition variables from threaded environments. State machines, object orientation and the actor model. Functional reactive programming. We’ve only scratched the surface for how to deal with concurrency in JavaScript.
In this book, I’ll help you understand the kinds of problems you’ll face writing asynchronous programs, what tools we have to help us and how to make best use of them, and take you on a tour of new ideas to further improve your code.