Skip to content

Lesson 02 NPM and Async Patterns

Mary Alice Moore edited this page Mar 29, 2024 · 1 revision

Lesson Page

In this lesson, the node package manager npm is introduced, along with async patterns (async/await), event processing, and streams. There are a couple of additional assignments to reinforce the idea of modules and asynchronous processing.

Hard Parts

Async/Await and .Then/.Catch, Try/Catch

Make sure the students feel comfortable with these ideas. In particular, it is good to emphasize the differences, in that the code after a .then block executes before the code in a .then block, but the code after await doesn’t execute until the await completes.

Event Emitter

Students experiment with the node event emitter API a bit during this lesson. The learning objective here is to develop students’ understanding of the fact that programs can be event-driven rather than procedural. Students can get a bit overwhelmed, perplexed, and confused by the event emitters API. If you encounter this, emphasize the fundamental learning goal here. Ask questions about websites (request -> response) or web pages (click -> event handler). Even if students have never written a web server or UI, these are technologies that everyone interacts with. Students will usually understand that it is important for us to be able to write software that responds to events coming from the outside world, as opposed to just running scripts from beginning to end. We won’t visit the event emitter API again; if the HTTP server stuff is clicking with students, but the event emitter is not, simply offer encouragement and reinforcement to the student. Ask questions that scaffold students’ understanding of the important learning objects instead of the minutia of the event emitter API.

Streaming

Again, very tricky content that we don’t revisit later in the course. Lay the validation and sponsorship on thick.

Of course, streams and buffers are a fundamental concept in computing and operating systems. They are present during any HTTP operation, file I/O, or interaction between a web application server and a database.

Try explaining streams and buffers with high-level analogies, since a high-level understanding is enough for students to walk away with. For example:

“Consider people waiting in line. Then the length of the line is the buffer size. Reading from the stream is like serving a customer. They are removed from the line. Writing to the stream is like someone new getting in line.”

It may be worthwhile to point out that most HTTP application servers do not deal with this problem at all; at least not insofar as pushing around very large files. They will typically defer problems like this to S3 or a CDN.