July 2018
Beginner
100 pages
2h 7m
English
Callbacks are functions that are executed asynchronously, or at a later time. Instead of the code reading top to bottom procedurally, asynchronous programs may execute different functions at different times based on the order and speed of earlier functions.
Since JavaScript treats functions like any other object, we can pass a function as an argument in another function and alter execute that passed-in function or even return it to be executed later.
We saw such a function previously when we were looking at the fs module in The Module System section. Let's revisit it:
const fs = require('fs');let file = `${__dirname}/temp/sample.txt`;fs.readFile(file, 'utf8', (err, data) => { if (err) throw err; console.log(data);});
Read now
Unlock full access