Callbacks Node.js App: Code-Along
Follow the instructions to create a small console app using Node.js.
Note: Although the language is JavaScript, this is actually the back-end of the stack. Node.js uses the same JavaScript as the front-end, but on the server side.
Setup (Repl.it)
- Log into repl.it
- Create a new Node.js project
- Name it "Callbacks App"
"Hello World" App
First, make a simple Node.js app that displays a "Hello World" message in the console.
- Create a new file named app.js in the current directory
- Open the file, and enter the following text:
console.log('Hello World');
- Create a new file named .replit in the current directory
- Open the .replit file, and enter the following text:
run = "node app.js"
- Click the "Run" button, and verify that the message appears in the console!
ASCII Art Bunny
Next, update the program so that it has something a little more interesting to display.
- Comment out the "Hello World" message
- In the app.js file, define a function named
drawBunny
(no parameters) - In the body of the
drawBunny
function, add aconsole.log
statement - Use template literals to create a multi-line bunny and log it to the console:
() () >(^ ^)< (___)
Code
function drawBunny() {
console.log(`
() ()
>(^ ^)<
(___)`);
}
Questions
Q: What will happen when this code is run?
A: Nothing! The
drawBunny
function is never called!
A Function with a Callback
- Underneath the definition for
drawBunny
, define another function namedrepeat
- Give the
repeat
function one parameter:callback
- This will be a callback - a variable that holds a function
- In the body of the
repeat
function, call thecallback
function- This will be whatever function is passed into the
repeat
function
- This will be whatever function is passed into the
- Underneath the definition for
repeat
, call therepeat
function - In the call for the
repeat
function, pass indrawBunny
as an argument
Code
function repeat(callback) {
callback();
}
repeat(drawBunny);
Questions
Q: What will happen when this code is run?
A: The
drawBunny
function will execute once because it is called once from therepeat
function!Q: Why are there no quotes around
drawBunny
?A: It is not a string, it is a variable pointing to the
drawBunny
function which is an object!
Making the repeat
Function Repeat
So far, the repeat
function only calls its callback once. Instead, it should have another parameter that determines how many times to call the callback.
- In the definition of the
repeat
function, add another parameter:numberOfTimes
- This parameter will determine how many times
repeat
should call the callback
- This parameter will determine how many times
- In the body of the
repeat
function, remove thecallback()
function call - Add a line declaring a variable named
i
with thelet
keyword- For more information on using
let
instead ofvar
, visit stack overflow - For simplicity,
let
is the modern approach to declaring variables in JavaScript
- For more information on using
- Under the declaration of
i
, create awhile
loop with the conditioni < numberOfTimes
- In the body of the
while
loop, call thecallback
function - Also in the body of the
while
loop, incrementi
by1
- Finally, update the call to
repeat
and pass in a second argument of3
Run the program, and verify that three bunnies appear!
Code
function repeat(callback, numberOfTimes) {
let i = 0;
while (i < numberOfTimes) {
callback();
i = i+1;
}
}
repeat(drawBunny, 3);
Repeating Another Function
The usefulness of callbacks becomes clearer when there are more possibilities. Defining another ASCII art function and passing it into repeat
should illustrate the importance of callbacks.
- Between the
drawBunny
definition and therepeat
definition, define a new function nameddrawChefHat
- In the body of the
drawChefHat
function, useconsole.log
to draw a chef hat to the console:_____ ( ) | | |___|
- Under the call to
repeat
, callrepeat
again - In the second
repeat
call, pass indrawChefHat
and2
as the arguments - Run the code to see both the bunny and the chef hat appear repeatedly!
Final Code
function drawBunny() {
console.log(`
() ()
>(^ ^)<
(___)`);
}
function drawChefHat() {
console.log(`
_____
( )
| |
|___|`)
}
function repeat(callback, numberOfTimes) {
let i = 0;
while (i < numberOfTimes) {
callback();
i = i+1;
}
}
repeat(drawBunny, 3);
repeat(drawChefHat, 2);