O'Reilly logo
live online training icon Live Online training

Asynchronous Programming with Node.js

Exploring the core Node features that enable high-volume, scalable applications

Chetan Karande

The asynchronous execution model is at the heart of Node.js internal architecture, enabling it to handle thousands of concurrent requests with a single CPU core, and process gigabytes of data with a tiny amount of RAM. Embracing the craft of asynchronous programming is therefore essential for Node.js developers for building modern large-scale applications on this platform. However, the related concepts could be intimidating due to a steep learning curve and related pitfalls often trip up developers causing applications to crash due to unhandled errors or difficult to detect memory leaks.

This training demystifies the asynchronous programming constructs offered by the JavaScript language and Node.js APIs, including:

  • callbacks
  • promises
  • async/await
  • async iterator
  • async generator
  • stream

Learners will discover the practical skills needed to be effective Node.js developers.

What you'll learn-and how you can apply it

By the end of this live, hands-on, online course, you’ll understand:

  • the pitfalls related to memory leaks associated with the asynchronous code
  • relatively new JavaScript language features such as async iterator, async generator

And you’ll be able to:

  • write asynchronous programs in a maintainable and comprehensible way
  • handle runtime errors gracefully from the asynchronous code
  • effectively use powerful Node.js streams to process large amount of data

This training course is for you because...

  • You’re a web developer with proficiency in JavaScript language and an understanding of basic Node.js development
  • You already work with Node.js but need to understand the newer asynchronous APIs

Prerequisites

  • Intermediate JavaScript skills with at least some experience developing Node.js applications

Recommended preparation:

Recommended follow-up:

About your instructor

  • Chetan Karande is a full stack web developer, security researcher, author, speaker at developer conferences. He is the author of Securing Node Applications (O’Reilly). He is the project leader for the OWASP NodeGoat project and contributor to multiple open source projects.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Opening (20 minutes)

  • Presentation: Review Node.js architecture choices and its implications on processing I/O, memory, and CPU intensive operations

Mechanisms for handling I/O operations (40 minutes)

  • Presentation: Overview of Node.js event loop and I/O execution mechanism using native OS async features and libuv Thread pool
  • Presentation: Asynchronous programming mechanisms: callbacks, promises, async functions and common coding pitfalls related to each
  • Exercise: Fix error handling implementation for an I/O operation
  • Q&A
  • Break (5 minutes)

Mechanisms for handling memory intensive operations (50 minutes)

  • Presentation: Review of Node.js stream types and controls that allow limiting memory usage while data processing
  • Presentation: Using Node streams with async generator and async iterator
  • Exercise: Fix the code to use stream API to optimize memory utilization
  • Q&A
  • Break (5 minutes)

Mechanisms for handling CPU intensive operations (30 minutes)

  • Presentation: Utilizing the multi-core system using worker thread and child_process APIs to execute tasks in parallel
  • Exercise: Fix the code to optimize CPU core utilization
  • Q&A

Mechanisms for implementing asynchronous logic (20 minutes)

  • Presentation: Order of execution among Node.js timer APIs, promises, and process.nextTick(); related queues and event loop execution phases
  • Presentation: Pitfalls related resource starvation associated with micro task queues prioritization and memory usage
  • Exercise: Identify the order of execution of asynchronous functions
  • Q&A

Recap, further learning, and wrap-up (10 minutes)