Atom
Atom
Finish Your Node App

Finish Your Node App

Master asynchronous calls and write programs that do what you intended

Node doesn’t wait for your database call to finish

TypeError: Cannot read property 'length' of undefined

at /home/server/app/routes/index.js:30:15
at Layer.handle [as handle_request] (/home/server/app/node_modules/express/lib/router/layer.js:95:5)
at next (/home/server/app/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/server/app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/server/app/node_modules/express/lib/router/layer.js:95:5)

...

You know how frustrating it is to try to make your Node program do what you want. The code you’ve written has no syntax errors, but running it has strange behavior like:

  • Node doesn’t wait for your database call to finish before rendering a page
  • you get Cannot read property of undefined when accessing your variable after a call
  • your HTTP calls fire at the same time, not after each other like you intended

“...just think asynchronously!” - But, how exactly?

You’re supposed to “think asynchronously” when programming in Node but it’s not exactly clear how to accomplish this. Examples in tutorials seem reasonable when reading them but translating them into your situation is difficult. You don't know which statement to write next.

Reading n-th Stack Overflow post, but your page still renders with zero results

Trying to find the official word on this leaves you dumbfounded. How come the official docs at nodejs.org have no word on how you’re supposed to program Node?

It looks like the real information is scattered across blogs, Github issues and Stack Overflow answers. Going that route takes forever - and you don’t which pieces would be the best use of your time.

Why doesn't this work

What if you could write the app that’s in your head

What if you were able to take the vision in your head and turn it into a working and running program. Imagine knowing exactly which statement to write next. You’d be the one in control of your app. You’d write programs that

  • finish database calls and only then continue further processing
  • have variables filled with values when you need them
  • have HTTP calls that run one at a time
  • You’d be able to express program logic in Node.js.
  • Every statement in your program would have a reason to exist.
  • Time spent working on your app would go to implementing new features.

It’s true asynchronous calls are a little hard to grasp, but it’s possible to make your text editor the place where working programs are written.

You’d know exactly which line to write next

The key to writing working programs is to understand how Node executes your calls. The internals of Node are predictable when you zoom out from the level of individual statements and for-loops. When you look at the system that takes over when you return from a top-level function you’ll start to understand how Node works.

Time spent on your app would go to implementing new features

When you understand what takes place behind the scenes, you're able to get the right mental model how to write Node programs. You'll be able to write programs that work as intended.

And that is exactly what you’ll learn in this book.

Atom
Atom
Finish Your Node App

Book that teaches right mental model for Node apps

When you grab this book, you’ll learn:

  • the role of a node programmer that is to start operations, register functions and let control back
  • the two-fold nature of starting operation in the background and receiving results later
  • execution order of calls, when callbacks are run
  • how to make things run at the same time or after each other
  • making use of waiting time
  • care about other computations taking place in your program
Right mental order

The book is written in a concise form that is perfect for busy people who don’t have too much spare time on their hands.

Execution order of calls

Exercises where you’ll learn to make calls run at same time or after each other

Theory alone is not enough if you don’t get to practice your newly learned skills. You’ll write five programs as exercise assignments. You get to build a news aggregator and a Twitter bot, call weather APIs and calculate cryptographic hash functions.

Exercises:

Breathing Relaxation Method

Breathing Relaxation Method

Fix Broken Code

Fix Broken Code

File Hasher

File Hasher

Twitter Weather Bot

Twitter Weather Bot

News Aggregator

News Aggregator

Table of contents:

What You Need to Know to Write Your First App

  • Initial User Code 3
  • Role of Node Programmer 3
  • System 5
  • Experiment 5

Excercise: Breathing Relaxation Method

System

  • System Calls Initial User Code 9
  • Waiting For Activity and Calling Registered Functions 10
  • Pseudo-code of System 10

Returning Control Back

  • Ways of Return Control Back 12

Excercise: Fix Broken Code

Reading files

  • File Read Program 18
  • Call Returns Without Results 19
  • Function is Called When Results Are In 19
  • Error Argument 19
  • Experiment 20

Excercise: File Hasher

Reading Code

  • Synchronously Called Function 26
  • Asynchronously Called Function 26
  • Experiment 27

Visualizing Program Execution

  • Visualizing First Program 28

Putting Dependent Logic Inside Callback

  • Reading Files 33
  • Making HTTP Calls 33
  • Experiment 34

Excercise: Twitter Weather Bot

Making Use of Waiting Time

  • Communicating with Network is Slow 40
  • Worker Threads 41
  • Making Use of Waiting Time 42
  • Experiment 43

Excercise: News Aggregator

Bonus: Caring for Other Computations

  • SetImmediate 49
  • Prime Numbers 49
  • Prime Numbers with Other Activity 50
  • Experiment 52

Bonus: Complete System

  • process.nextTick() 53
  • Complete System 53
  • Experiment 55
Atom
Atom

Grab the book and turn app from your head into a working node program

  • Book pdf (56 pages)
  • 10 chapters
  • 5 excercises
  • Source code to examples zip
Atom
Node.js File Uploads Course

Q&A

Should I know JavaScript before reading the book?

You should know entry level JavaScript and have some previous experience in programming. In terms of Node, you don't need in-depth experience with Node. All advanced Node topics are introduced with links to additional material.

Does the book teach Promises or async/await?

No. This book is for learning asynchronous calls or the operating principles at the heart of Node. The book uses callbacks and plain Node. Learning Promises and async/await is recommended after reading this book and is much easier when you've got the right mental model for Node.