The previous section illustrates how a function can be passed to another function as an argument. This section takes the opposite perspective to write functions that can take other functions as arguments.
Our first example will be a generic input validator. It asks the user for some
input, using the prompt
parameter for the text of the question. A second
parameter receives a function that does the actual work of validating the
input.
Example
1const input = require('readline-sync');
2
3function getValidInput(prompt, isValid) {
4
5 // Prompt the user, using the prompt string that was passed
6 let userInput = input.question(prompt);
7
8 // Call the boolean function isValid to check the input
9 while (!isValid(userInput)) {
10 console.log("Invalid input. Try again.");
11 userInput = input.question(prompt);
12 }
13
14 return userInput;
15}
16
17// A boolean function for validating input
18let isEven = function(n) {
19 return Number(n) % 2 === 0;
20};
21
22console.log(getValidInput('Enter an even number:', isEven));
Sample Output
Enter an even number: 3
Invalid input. Try again.
Enter an even number: 5
Invalid input. Try again.
Enter an even number: 4
4
When we call getValidInput
on line 22, we pass it the string
'Enter an even number:'
, which gets assigned to the prompt
parameter.
Notice that we also pass in the function isEven
(with no arguments).
This gets assigned to the isValid
parameter.
The function getValidInput
handles the work of interacting with the user, while allowing the validation logic to be customized. This separates the different concerns of validation and user interaction, sticking to the idea that a function should do only one thing. It also enables more reusable code. If we need to get different input from the user, we can simply call getValidInput
with different arguments.
Example
This example uses the same getValidInput
function defined above with a different prompt and validator function. In this case, we check that a potential password has at least 8 characters.
1const input = require('readline-sync');
2
3function getValidInput(prompt, isValid) {
4
5 let userInput = input.question(prompt);
6
7 while (!isValid(userInput)) {
8 console.log("Invalid input. Try again.");
9 userInput = input.question(prompt);
10 }
11
12 return userInput;
13}
14
15let isValidPassword = function(password) {
16
17 // Passwords should have at least 8 characters
18 if (password.length < 8) {
19 return false;
20 }
21
22 return true;
23};
24
25console.log(getValidInput('Create a password:', isValidPassword));
Sample Output
Create a password: launch
Invalid input. Try again.
Create a password: code
Invalid input. Try again.
Create a password: launchcode
launchcode
Try It!
getValidInput
function to ensure user input starts with "a".Another common example of a function using another function to customize its behavior is that of logging. Real-world applications are capable of logging messages such as errors, warnings, and statuses. Such applications allow for log messages to be sent to one or more destinations. For example, the application may log messages to both the console and to a file.
We can write a logging function that relies on a function parameter to determine the logging destination.
Example
The logError
function outputs a standardized error message to a location determined by the parameter logger
.
1let fileLogger = function(msg) {
2
3 // Put the message in a file
4
5}
6
7function logError(msg, logger) {
8 let errorMsg = 'ERROR: ' + msg;
9 logger(errorMsg);
10}
11
12logError('Something broke!', fileLogger);
Let's examine this example in more detail.
There are three main program components:
fileLogger
, which takes a string argument, msg
. We have not discussed writing to a file, but Node.js is capable of doing so.logError
. The first parameter is the message to be logged. The second parameter is the logging function that will do the work of sending the message somewhere. logError
doesn't know the details of how the message will be logged. It simply formats the message, and calls logger
.fileLogger
.This is the flow of execution:
logError
is called, with a message and the logging function fileLogger
passed as arguments.logError
runs, passing the constructed message to logger
, which refers to fileLogger
.fileLogger
executes, sending the message to a file.This example can be made even more powerful by enabling multiple loggers.
Example
The call to logError
will log the message to both the console and a file.
1let fileLogger = function(msg) {
2
3 // Put the message in a file
4
5}
6
7let consoleLogger = function(msg) {
8
9 console.log(msg);
10
11}
12
13function logError(msg, loggers) {
14
15 let errorMsg = 'ERROR: ' + msg;
16
17 for (let i = 0; i < loggers.length; i++) {
18 loggers[i](errorMsg);
19 }
20
21}
22
23logError('Something broke!', [fileLogger, consoleLogger]);
The main change to the program is that logError
now accepts an array of functions. It loops through the array, calling each logger with the message string.
As with the validation example, these programs separate behaviors in a way that makes the code more flexible. To add or remove a logging destination, we can simply change the way that we call logError
. The code inside logError
doesn't know how each logging function does its job. It is concerned only with creating the message string and passing it to the logger(s).