Skip to content

Asynchronous code, callbacks

Course: Asynchronous Javascript, Callbacks and more - chapter 8

Let’s look at the code to read from a file asynchronously. Here, we pass in a function as an argument. This function is called a callback since the function gets called back when the file is read

const fs = require("fs");
fs.readFile("a.txt", "utf-8", function (err, contents) {
console.log(contents);
});

setTimeout is another asynchronous function that executes a certain code after some time

function run() {
console.log("I will run after 1s");
}
setTimeout(run, 1000);
console.log("I will run immedietely");