Returning Values from Callbacks
You'll learn why return values inside callbacks often don't work the way you'd expect, and when they actually matter.
The Core Misunderstanding
When you write return inside a callback function, that value is returned to whoever called the callback — not to the original function that set everything up. This is a critical distinction that trips up many developers.
Think of it like leaving a voicemail. When you hang up and say "I've left them a message," that doesn't mean the person has heard your message yet, let alone responded to you directly. Your "return value" goes to the answering machine, not to you.
When Return Values Matter
Return values in callbacks are only meaningful if the function invoking the callback actually uses them:
// The callback's return value IS used
const numbers = [1, 2, 3];
const doubled = numbers.map(function(num) {
return num * 2; // map() uses this return value
});
console.log(doubled); // [2, 4, 6]
Here, map() calls your callback and collects each return value into a new array.
When Return Values Are Ignored
setTimeout(function() {
return "I'm done!"; // This return value goes nowhere
}, 1000);
The setTimeout function doesn't do anything with what your callback returns. The value simply evaporates. You'd need to use other mechanisms (like modifying an outer variable or calling another function) to communicate results.
Key Takeaway: A callback's return value is only useful if the function calling that callback is designed to receive and use it — returning from a callback doesn't send the value back to your original calling code.