Node.js FAQ: Top Questions
7. What is a callback function in Node.js?
A callback function is a function passed as an argument to another function, executed after an asynchronous operation completes, often following the error-first pattern.
-
Error-First:
Callback receives
erroras the first parameter, then data. - Issues: Can lead to "callback hell" (nested callbacks).
- Alternatives: Promises or async/await for cleaner code.
- Use Case: Handling async operations like file I/O, HTTP requests.
