26

There are a lot of questions within the 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:

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 and ?

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.

13
  • 8
    This ultimately is not limited to callbacks, but to anywhere that it is intended to pass the function object instead of the result of the function. This is a typo, and should just be closed as such. There's no way to generalize such a question that users in said situations will find it.
    – user4639281
    Commented Jan 1, 2018 at 1:01
  • 44
    A "typo" implies that the asker will immediately understand the mistake when pointed out to them, and they just mis-typed when coding and didn't notice. This situation is definitely not a typo, but instead shows the asker is unaware of a fundamental concept in the language. Closing as a duplicate which has an explanation is definitely better than closing as a typo.
    – Dave
    Commented Jan 1, 2018 at 15:37
  • 2
    @Dave Every user I have ever encountered who has run into this falls into the first category. They didn't know that they were supposed to pass the reference, not the value, but they know the difference between calling and passing a function. I do not suggest the typo close reason lightly, and am usually highly critical of its use (see: Resolved in a manner unlikely to help future readers), but this is a typo.
    – user4639281
    Commented Jan 1, 2018 at 16:47
  • 11
    There are so many newcomers to Javascript, I think a lot of them don't even realize you can pass a function around, rather, merely executing functions for them is the norm. Closing as a typo is not going to educate them, and they might therefore continue to ask similar low quality questions.
    – Dexygen
    Commented Jan 2, 2018 at 10:46
  • How about add your detailed answer to one of the older questions, link to it from the new (and maybe some of the other old) questions?
    – user128511
    Commented Jan 3, 2018 at 9:50
  • Should we create two answers for the canonical? One with the simple, short answer of "It expects the function itself, not the result of invoking the function" and another that goes into the details of how javascript treats functions?
    – ryanyuyu
    Commented Jan 3, 2018 at 17:12
  • 1
    I find this one to be the best: stackoverflow.com/questions/20890943/… it's short, concise, straight to the point, not an essay, and already well upvoted/viewed/used as a dupe target. And as a bonus, it's correctly closed.
    – Kevin B
    Commented Jan 3, 2018 at 17:33
  • @KevinB Agreed. I've been using it, but it sometimes leads to the OP commenting "I'm not using 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.
    – Andrew Li
    Commented Jan 3, 2018 at 17:36
  • there's an easy fix to that.... don't
    – Kevin B
    Commented Jan 3, 2018 at 17:36
  • @KevinB But then they don't learn anything and run the risk of asking the same question over and over. And yes, you could say it's not my problem they don't take the time to read it but would potentially be creating more crap questions so I find leaving comments a better alternative.
    – Andrew Li
    Commented Jan 3, 2018 at 17:38
  • That's a rabbit hole. If you do that every time you run into a duplicate you'll never get anything done and they'll never learn to research for themselves. The same can be said when you close a setTimeout(foo(),10) question with the generic one you're looking for.
    – Kevin B
    Commented Jan 3, 2018 at 17:39
  • @KevinB I wouldn't say every time I comment but when the title is specific in this case, I'd rather create a dupe that's more generic to avoid commenting at all in the first place.
    – Andrew Li
    Commented Jan 3, 2018 at 17:41
  • a large problem with questions that sortof delv into this topic is that it's a symptom of many other common problems. For example, async actions in for loops, returning from async callbacks, passing parameters to callbacks, etc, each of which have appropriate duplicates. i don't think a cannoical that explains how to generically use a callback function would really cover the majority of real cases. It would fix one piece of the question, but not the overall problem.
    – Kevin B
    Commented Jan 3, 2018 at 18:40

4 Answers 4

6

Yes we should create a new canonical with a short, descriptive answer. It should have a good explanation, but be kept short for readability (or at least have a TL;DR).

This way all the new and current posts can be closed as duplicates of this instead of as typos.

7
  • This answer is meant to asses whether the community approves of a new canonical or not. Please vote accordingly.
    – River
    Commented Jan 3, 2018 at 0:04
  • I agree there should be a canonical but it should probably be created with a good explanation; and maybe a tl;dr if it really is that long. People should know why not just the fix.
    – Andrew Li
    Commented Jan 3, 2018 at 0:13
  • One already exists. we don't need a more specific one
    – Kevin B
    Commented Jan 3, 2018 at 16:59
  • 1
    @KevinB Could you point to a good widely applicable one?
    – Andrew Li
    Commented Jan 3, 2018 at 17:06
  • @Li357 stackoverflow.com/help/on-topic
    – Kevin B
    Commented Jan 3, 2018 at 17:07
  • bsides, there's nothing wrong with pointing to multiple duplicates if you don't think one is enough to fully describe what's wrong.
    – Kevin B
    Commented Jan 3, 2018 at 17:09
  • All of the ones you've linked to are perfectly fine.
    – Kevin B
    Commented Jan 3, 2018 at 17:11
3

I've had a post it note to

Write a canonical "Callback Executed Immediately" question and answer on Stack Overflow

for over a year.

"Calling functions with setTimeout()" has been my go-to duplicate close reason, but I always have to post a disclaimer that the answer is relevant even though the question might not appear to be an exact match.

I think it's worthwhile that we come up with a well-worded generic question with the explicit intent to thoroughly cover the topic.

0

What about How can I pass a parameter to a setTimeout() callback?? Although the general issue is the typo, the above question may more fit the intent of the questions.

1
  • 1
    The only thing I don't like about that is that the accepted answer uses the anonymous method form. It certainly works and is typically how I do things, but isn't...really the best case for what the asker is looking for. A decent candidate otherwise. Commented Jan 3, 2018 at 0:31
-1

There's When do I use parenthesis and when do I not?. The accepted answer is brief and to the point regarding what parentheses are used in JavaScript, whereas the highest-voted answer details the specific use case with setTimeout. I think it also (obliquely, perhaps) points out the "typo" nature of the question.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .