There are a lot of questions within the javascript tag that have to do with passing callback functions wrong, for example doing this:
setTimeout(foo(), 3000);
Instead of, correctly:
setTimeout(foo, 3000);
Where foo
is a function that does not return a function, and the OP wants to pass foo
as the callback function. I can't find a good genericized post that I can mark these duplicates of. Some I've looked at include:
- Calling functions with setTimeout() (Not genericized enough, some askers won't realize it's related to their problem, can't just be
setTimeout
- javascript setTimeout() not working (Again about just
setTimeout
, no in-depth explanation present) - Why does click event handler fire immediately upon page load? (Only really about event handlers, has explanation though not much)
Is there a single post that explains that callback references must actually be passed, not a callback call? If not, should it be created, considering there's a huge volume of them cropping up in javascript and react?
Regarding the relative volume of these posts, here are some I encountered just today:
And a few more I can't find, but these questions are terribly common with many, many duplicate answers. And I disagree they are typos, sure they can be explained away by a comment but it shows a fundamental misunderstanding not just restricted to callbacks. Function calls are evaluated then their value passed to an enclosing function as an argument, and functions can be passed to others because they are first class objects.
setTimeout
why did you mark this as a duplicate?!?!". Then you spend a good 10 minutes explaining to them that it's the same fundamental issue.