Finish Your Node App
Master asynchronous calls and write programs that do what you intended
After three rewrites your Node program still doesn’t do what you want!
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
- your get TypeError: undefined is not an object when accessing your variables after a call
- your HTTP calls fire at the same time, not after each other like you intended
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.
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 even know where to start.
You know it’s possible to make real-world Node apps, there’s plenty of existing projects out there, but you’re not able to pull it off yourself.
What if you could turn the plan in your head into working code?
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 every time 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.
Grab Finish Your Node App book and write programs that do what you intended
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.
When you understand what takes place behind the scenes, you're able to get the right mental model how to write Node programs. And that is exactly what you’ll learn in 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
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.
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.
Table of contents
What You Need to Know to Write Your First App
- Initial User Code
- Role of Node Programmer
- System Calls Initial User Code
- Waiting For Activity and Calling Registered Functions
- Pseudo-code of System
Returning Control Back
- Ways to Return Control Back
- File Read Program
- Call Returns Without Results
- Function Is Called When Results Are In
- Error Argument
- Synchronously Called Function
- Asynchronously Called Function
Visualizing Program Execution
- Visualizing First Program
Putting Dependent Logic Inside Callback
- Reading Files
- Making HTTP Calls
Making Use of Waiting Time
- Communicating with Network is Slow
- Worker Threads
- Making Use of Waiting Time
Bonus: Caring for Other Computations
- Prime Numbers
- Prime Numbers with Other Activity
Bonus: Complete System
- Complete System
Q & A
Does the book teach async-library, Promises, generators or async/await?
No. This book is for learning asynchronous calls and the operating principles at the heart of Node. The book uses callbacks and plain Node. Learning async-library, Promises, generators and async/await is recommended after reading this book and is much easier after you've mastered the basics.
Does the book use ES6?