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
drawBunnyfunction, add aconsole.logstatement - 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
drawBunnyfunction is never called!
A Function with a Callback
- Underneath the definition for
drawBunny, define another function namedrepeat - Give the
repeatfunction one parameter:callback- This will be a callback - a variable that holds a function
- In the body of the
repeatfunction, call thecallbackfunction- This will be whatever function is passed into the
repeatfunction
- This will be whatever function is passed into the
- Underneath the definition for
repeat, call therepeatfunction - In the call for the
repeatfunction, pass indrawBunnyas an argument
Code
function repeat(callback) {
callback();
}
repeat(drawBunny);
Questions
Q: What will happen when this code is run?
A: The
drawBunnyfunction will execute once because it is called once from therepeatfunction!Q: Why are there no quotes around
drawBunny?A: It is not a string, it is a variable pointing to the
drawBunnyfunction 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
repeatfunction, add another parameter:numberOfTimes- This parameter will determine how many times
repeatshould call the callback
- This parameter will determine how many times
- In the body of the
repeatfunction, remove thecallback()function call - Add a line declaring a variable named
iwith theletkeyword- For more information on using
letinstead ofvar, visit stack overflow - For simplicity,
letis the modern approach to declaring variables in JavaScript
- For more information on using
- Under the declaration of
i, create awhileloop with the conditioni < numberOfTimes - In the body of the
whileloop, call thecallbackfunction - Also in the body of the
whileloop, incrementiby1 - Finally, update the call to
repeatand 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
drawBunnydefinition and therepeatdefinition, define a new function nameddrawChefHat - In the body of the
drawChefHatfunction, useconsole.logto draw a chef hat to the console:_____ ( ) | | |___| - Under the call to
repeat, callrepeatagain - In the second
repeatcall, pass indrawChefHatand2as 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);