December 25, 2014

You Dont Know JS: Async and Performance: Book Review

I had this book for a while and finally read it. Great stuff about asynchronous programming in general. They are really tips about how to think about Async and Promises.

The books author is Kyle Simpson. The book is about asynchronous programming in JavaScript. There are 3 chapters, ‘Asynchrony: Now and Later’, ‘Callbacks’ and ‘Promises’. Each chapter feeds into the next and it is the story of how you can rewire your JavaScript to make more sense.

Asynchrony: Now and Later

The book starts off by breaking down what JavaScript is, and how it is one big event loop. This loop allows callbacks to queue up to be executed asynchronously. The book then compares this with parallel programming and shows how async code in JavaScript is more deterministic than what threads would be, but there are still non-determinism within JavaScript. This can lead to race conditions. The last bit of the chapter then says to keep your code simple, breaking your code into small chunks will help out.

Callbacks

Callbacks were born out of making small, reusable functions. This chapter starts talking about callbacks and some of the drawbacks they have. Issues of ‘callback hell’, events firing too early, or late or never. The unfortunately truth is that they make your code ugly and hard to read. This leads into Promises.

Promises

Promises help change the inversion of control in callbacks. This then makes callbacks readable, and thus code easier to maintain. The rest of the chapter goes over how awesome promises are and how they can mitigate callback hell, and solve those issues with firing too early or late.

Ultimately, this book has helped me to think about asynchronous programming a bit differently. One, thing is to break up your data so that callbacks are fast and don’t tie up the UI thread. The other thing is how Promises are mimicked in other languages, like C#s async and Task Parallel Library. A neat book, short but good read to refresh my brain on JavaScript and asynchronous programming.