August 2017
Beginner
374 pages
10h 41m
English
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(err => callback(err))}
A function like this can be called by passing a callback function to it:
requestAndDo((err, data) => { if (err) throw err // ...handle data...})
Similarly, we could write a ...
Read now
Unlock full access