January 2020
Intermediate to advanced
470 pages
11h 13m
English
Probably the most used example of functions passed as first-class objects has to do with callbacks and promises. In Node, reading a file is accomplished asynchronously with something like the following code:
const fs = require("fs");fs.readFile("someFile.txt", (err, data) => { if (err) { console.error(err); // or throw an error, or otherwise handle the problem } else { console.log(data.toString()); // do something with the data }});
The readFile() function requires a callback—in this example an anonymous function—that will get called when the file reading operation is finished.
A better way is using promises; read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise ...
Read now
Unlock full access