Session Plan
This guide is for mentors. It outlines how to run the session, what to emphasize, and why we introduce certain terminology.
Session Goals
By the end of this session, trainees should:
Understand functions as first-class values
Use
reduceconfidently for aggregationBuild their own higher-order functions (myForEach, myMap)
Understand how delayed callbacks work (setTimeout, fs.readFile)
Write callbacks for
setTimeoutandfs.readFileUnderstand the error-first callback pattern
Glossary Goals
Callback
Central concept. Week 1 used them implicitly; now we name them explicitly.
Accumulator
Essential for understanding reduce. Common in documentation.
Pure function
Foundation for understanding why bugs with delayed code happen.
Immutability
Connects to state management and safety with delayed code.
Error-first callback
Node.js convention. They'll see this pattern constantly.
Delayed execution
Fundamental concept for backend development.
Session Outline
1. Introduction
Recall Week 1: forEach, map, filter all take functions as arguments
"Those functions are callbacks - but they run immediately"
This week: callbacks that run later
The cafe metaphor: a waiter doesn't stand at the kitchen waiting for one order
Use: Slides (introduction sections)
2. Functions as Values
Functions are "first-class citizens" - treated like any other value
Live code: assign function to variable, call via variable
Live code:
functionRunner(fn)that calls the passed function - core callback patternLive code: array of functions, call each
Live code: function that returns a function (factory pattern)
Key point: "You can pass functions around like data. This is what makes callbacks possible."
Exercises: Part 1 (exercises 1-4)
3. reduce Method
Connect to Week 1: map transforms, filter selects, forEach does side effects
reduce: the "build up" pattern
Walk through the accumulator concept with a simple sum
Live code: sum of stockCount
Live code: group by origin (object accumulator)
Key point: "reduce lets you build any single value from an array - a number, object, string, even another array."
Exercises: Part 2 (exercises 5-8)
Break 1
4. Callbacks - Synchronous
Key insight: the function you pass to map IS a callback
"Let's build our own forEach to see how callbacks work under the hood"
Live code: implement
myForEach(array, callback)Live code: implement
myMap(array, callback)
Key point: "Building these yourself demystifies how array methods work. They're just functions calling your callback."
Exercises: Part 3 (exercises 9-11)
5. setTimeout & Delayed Callbacks
Now the twist: what if the callback runs later?
Show code with surprising output order - let trainees predict first
Explain: JavaScript continues running, callback runs when timer fires
Live code:
runAfterDelay(delay, callback)utilityLive code: simulated database lookup with delay
Key point: "The callback doesn't block. JavaScript keeps running. This is how backends serve many users at once."
Exercises: Part 4 (exercises 12-15)
Break 2
6. File System Callbacks
Real backend example: reading files
Introduce error-first pattern:
(error, data)Live code: read a JSON file, parse it, use the data
Emphasize: always check error first
Key point: "Node uses error-first callbacks everywhere. Always handle errors before using data."
Exercises: Part 5 (exercises 16-17)
7. Wrap-up
Recap: functions as values, reduce, callbacks, delayed callbacks
Preview Week 3: "Callback nesting gets ugly. Promises solve this."
Introduce the assignment
Teaching Tips
Managing exercises with mixed skill levels
The exercises are intentionally numerous - trainees progress at different speeds. Here's how to manage this:
Let trainees work on Part 1 - observe how far each gets individually
Solve one exercise in plenum - pick one that weaker trainees struggled with but stronger trainees just finished. This way everyone benefits
Move on to Part 2 - weaker trainees skip remaining Part 1 exercises (they can revisit later)
Repeat for each part
Exercises marked with ⭐ are optional stretch goals. If your fastest trainees finish all exercises while others haven't solved the first one, that part needs an additional challenge - let us know, or create a PR with a creative challenging exercise!
The callback revelation
Many trainees have used map/filter without realizing they're passing callbacks. Make this explicit: "You've been using callbacks all along!"
Show the surprising output order
For setTimeout, always ask trainees to predict the console.log order before running. The surprise helps the concept stick.
Use the tea data
Continue using the tea shop context. Show how reduce solves problems they encountered in Week 1 (like grouping teas by origin).
Connect to backend reality
Regularly remind them: "This is why servers don't freeze when handling multiple requests. They start operations and handle results via callbacks."
Materials
Slides - Reveal.js presentation
Exercises - In-session exercises
Assignment - Take-home work
Tea Data - Shared dataset that you can find in your assignment repo
Last updated