Table of Contents
The function `greet` is designed to print a greeting message with the provided `name`.
2.1 Higher-Order Functions
A higher-order function is a function that takes one or more functions as arguments, returns a function as its result, or both. In other words, higher-order functions operate on other functions, either by taking them as arguments or by returning them.
Here’s an example:
The function `callThreeTimes` is a higher-order function because it takes a function `f` as an argument.
The function `innerFunction` is a closure that is defined inside `outerFunction` and has access to `outerFunction`’s scope and the global scope.
3. Callback Functions and Promises
3.1 Callback Functions
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
Here, the function that logs “Finished my homework” is a callback function that’s passed to `doHomework`.
If the room is clean, the promise is resolved, and the function passed to `then` is executed. If the room isn’t clean, the promise is rejected, and the function passed to `catch` is executed.
4. The Power of Function Composition
Function composition is the process of combining two or more functions to produce a new function. Composing functions together is like snapping together a series of pipes for our data to flow through.
Here, `compose` takes in two functions `f` and `g` and returns a new function that represents the composed f(g(x)) function.