Callback Interview Questions in JavaScript
Callbacks are a fundamental interview topic. Here are the most asked questions with answers.
Callback Interview Questions in JavaScript
Callbacks are fundamental to async JavaScript. Here are the most common interview questions with answers.
Q1: What is a callback?
A function passed as an argument to another function, to be called later. Used for async operations (setTimeout, fetch, events) and synchronous operations (map, filter, reduce).
Q2: Are all callbacks asynchronous?
No. map/filter/reduce call their callbacks synchronously. setTimeout, event listeners, and fetch call their callbacks asynchronously. The timing depends on the calling function.
Q3: What is callback hell?
Deeply nested callbacks that are hard to read and maintain:
fetchUser(id, (user) => { fetchPosts(user, (posts) => { fetchComments(posts[0], (comments) => { render(user, posts, comments); }); }); }); `` **Fix**: Use promises (chain `.then` calls) or async/await (flatten to synchronous-looking code). ### Q4: What is inversion of control in callbacks? When you pass a callback to a function, you give up control of when and how it is called. The function might call it multiple times, not at all, or with unexpected arguments. Promises solve this by guaranteeing the callback is called once with the right result or error. ### Q5: How do you handle errors with callbacks? The Node.js convention: the first argument is an error: ```js fs.readFile("file.txt", (err, data) => { if (err) { console.error(err); return; } console.log(data); }); `` Always check for the error first. Promises use `.catch` instead. ### Q6: What is the output? ```js console.log("1"); setTimeout(() => console.log("2"), 0); console.log("3"); `` **Answer**: `1, 3, 2`. The callback goes to the macrotask queue and runs after the synchronous code. ### Q7: How do you run callbacks in parallel? ```js Promise.all([fetchA(), fetchB(), fetchC()]).then(([a, b, c]) => { console.log(a, b, c); }); `` With plain callbacks, you would need a counter: ```js let done = 0; const results = []; function check() { if (++done === 3) console.log(results); } fetchA((a) => { results[0] = a; check(); }); fetchB((b) => { results[1] = b; check(); }); fetchC((c) => { results[2] = c; check(); }); `` Promises are cleaner. ### Q8: How do you promisify a callback-based function? ```js function promisify(fn) { return (...args) => new Promise((resolve, reject) => { fn(...args, (err, result) => { if (err) reject(err); else resolve(result); }); }); } const readFileP = promisify(fs.readFile); readFileP("file.txt").then(data => console.log(data)); `` ### The Takeaway Callback interview questions test: the definition, sync vs async, callback hell (and the promise/async-await fix), inversion of control, error handling (Node.js convention), ordering (sync first, then microtasks, then macrotasks), parallel callbacks, and promisification.
Deeply nested callbacks that are hard to read and maintain. Each async operation depends on the previous one, leading to a pyramid shape. Fix with promises (chain .then calls) or async/await (flatten to synchronous-looking code).
When you pass a callback to a function, you give up control of when and how it is called. The function might call it multiple times, not at all, or with unexpected arguments. Promises solve this by guaranteeing the callback is called once.
The Node.js convention: the first argument is an error. Always check if (err) first and handle it. In promises, use .catch instead. Never ignore the error argument in a callback.
No. map, filter, and reduce call their callbacks synchronously. setTimeout, event listeners, and fetch call their callbacks asynchronously. The timing depends on the calling function, not the callback itself.
Wrap it in a Promise. Call the original function with a callback that calls reject on error and resolve on success. Return the Promise. Node.js has util.promisify for this.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

