Junior
Which operations and functions in the code are asynchronous?
sobes.tech AI
Answer from AI
In Node.js, asynchronous operations are those that do not block the main execution thread and allow work to continue while a long task is in progress. Such operations include:
- Input/Output (I/O): reading and writing files, working with databases, network requests (HTTP, TCP).
- Timers: setTimeout, setInterval.
- Working with streams.
- Asynchronous functions and promises, which make writing asynchronous code more convenient.
Example of asynchronous file reading:
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Here, the file reading is asynchronous, and the main thread is not blocked while the file is being read.