In Node.js's fs (filesystem) module, some functions have both a synchronous version which returns results directly, and an asynchronous version which passes the result to a specified callback.
In this example I'm just going to use callbacks, since I think it's easier to start with the fundamentals and introduce promises (.then() and related) once you understand the basics.
Reading a file synchronously:
let fileData = fs.readFileSync("data.txt");
console.log(fileData);
console.log("next line");
/* OUTPUT:
>> data from text file ...
>> next line
*/
Reading a file asynchronously:
fs.readFile("data.txt", function(fileData) {
console.log(fileData);
});
console.log("next line");
/* OUTPUT:
>> next line
>> data from text file ...
*/
With both functions, it takes some time to read in the data from the text file. In the synchronous case, all execution is paused until the file is done being read in. Then, once it's done, it moves on to the next line.
In the asynchronous case, the task is started in the background, but we don't wait for it. We just move onto the next line, and keep going. When the task completes, it will call the function we specified inline, and at that point we can actually use the file data.
The synchronous code is easier to work with, since you won't need to think about callbacks and stuff. But no other code will get executed, so it you do this often, you'll have bad performance. And in the browser if you do something synchronously (e.g. a web request) the user won't be able to interact with the page until the operation completes.