Mastering Node.js is Mastering Async

Kyle
3 min readAug 26, 2014

--

I started with Javascript a few years ago when a website I was building just couldn’t be managed all from the backend. Actually, Scratch that. My first intro to Javascript was in 1999 — I was building my High School’s website (this was a job for a student back in the day) and they wanted a wacky hover effect that was cutting edge at the time. I did it but I had no idea how it worked. I didn’t touch Javascript for about 8 years due to the trauma.

Anyway, my serious learning did start with trying to get just enough Javascript to do what I wanted to do with a primarily back-end oriented site. I started with MooTools and followed any tutorial I could find. I distinctly remember being puzzled by the amount that seemed like magic. I started to get decent at it — elements were wizzing across the screen and I was manipulating elements on the page with ease. Then I got to the whole ajax thing. Easy enough until I got to trying to make something work in the right order. Callbacks were black magic and I was no black magician.

My initial [wrong] understanding was that callbacks were some sort of multitasking, flying in the face of the mantra that Javascript is single thread. After about a month of fiddling, I understood the true nature of how asynchronous, single-threaded programming worked. As I started to do front-end work a new job, I was the only one on staff that understood how callbacks worked. On my first day I was handed a bunch of front-end javascript that had dozens of magic-number based setTimeouts. It was a nightmare. I untangled that mess into a series of callbacks and was a hero even though it was a really basic task.

That same company got more ambitious with their Javascript-based sites with me onboard and the situation of multiple callbacks arose. I started learning promises. It worked fine but I always felt the implementation was a bit clunky. On another project, I needed to n number of asynchronous callbacks and discovered Async. It was amazing how quickly it solved the problem at hand. About the same time I started my learning journey with Node. My first code was garbage — the indent levels were out of control, but it worked.

Node.js + Async

Once I started to seriously consider the structure of my code, I saw more uses of the Async library. First, everything was a `async.series` or `async.parallel` — useful structures, sure. But there was so much more. When I figured out `async.waterfall` and `async.seq` my code was so much easier to read and understand. There is magic in that module and isn’t in any specific trick, but rather the new thinking that it opens up.

I really can’t say enough great things about how the Async module has changed the way I write code. If I were instructing someone to start learning Javascript (front-end or back-end), I would start with Node.js and the Async module. The garbage that I went through to learn the correct patterns were uncalled for — get new programmers started on the right foot and build up complexity. I’m not saying every project should include Async, but rather that Async has so many nifty structures and patterns that it is a great place to begin. It offers a great variety of tools rather than the single pattern that front-end programming examples and tutorials seem to push.

--

--

Kyle

Developer of things. Node.js + all the frontend jazz. Also, not from Stockholm, don’t do UX. Long story.