Explain the concept of non-blocking I/O in Node.js.

Beginner

Answer

Non-blocking I/O means that I/O operations (like reading files, database queries, network requests) don't stop the execution of other code. Instead of waiting for an operation to complete, Node.js continues executing other code and handles the I/O result when it's ready.

// Non-blocking example
const fs = require('fs');

console.log('Start reading file');
fs.readFile('large-file.txt', (err, data) => {
    console.log('File read complete');
});
console.log('This executes immediately');

// Output: Start reading file, This executes immediately, File read complete

This allows Node.js to handle thousands of concurrent connections efficiently.