Creating higher-order functions
There are two kinds of higher-order functions:
- Functions that take a function as an argument (for example, a callback pattern)
- Functions that return a function as their result
So, higher-order functions are simply functions that deal with functions. Higher-order reducer functions (also called higher-order reducers) are functions that wrap (take and return) reducer functions. We will talk more about these later.
Functions as arguments
The first type of higher-order functions are functions that take other functions as arguments:
function someFn (fn) { ... }You might be familiar with this from the callback pattern, which used to be very common when working with asynchronous JavaScript. We pass a callback function that takes an error object as the first argument and the data as the second. If no error happens, the first argument will be null:
function requestAndDo (callback) {
return fetch('http://localhost:1234')
.then(data => callback(null, data))
.catch...