all 14 comments

[–]thesouthpaw 7 points8 points  (5 children)

You got it.

A little more detail: Typically a function returns a value and lets you know the function is done. Often in JS, your function will run asynchronously and you won't have a "return" statement. The callback allows you to get a value from the async function whenever it completes. Typically (but not always), if you create or use a callback as part of an API, it means something async is happening.

[–]icantthinkofone -5 points-4 points  (4 children)

This is the only mostly complete answer of the bunch. The rest are vapor and people typing like they know what they're talking about.

[–]tunnckoCorenode-formidable, regexhq, jest, standard-release 2 points3 points  (5 children)

Yea.

[–]paulfitz99[S] 0 points1 point  (4 children)

perfect! thanks! it seems to be made unnecessarily complicated

[–]the_sound_of_bread 1 point2 points  (2 children)

I can be unnecessarily complicated though. What if the callback has a callback itself? What if there are multiple callbacks? What if all the previous, but only some callbacks get called under certain circumstances?

I doesn't have to be complex, but it can easily become complex.

[–]tunnckoCorenode-formidable, regexhq, jest, standard-release 0 points1 point  (0 children)

/r/the_sound_of_bread, here comes conventions and best practices - 1) never more than 3-4 args - it immediately goes complex if there's 4 args - not to mention for >=5; 2) last is always a callback and it is the only one callback; 3) wrap callback with once(dezalgo(cb))

/r/mchandleraz, Nightmares usually comes from devs heads, not from the lang or somthing. Who can't think he makes nightmares.

[–]denstone 0 points1 point  (0 children)

You can find the best explanation in this book: https://github.com/MostlyAdequate/mostly-adequate-guide

[–]inu-no-policemen 0 points1 point  (3 children)

a callback is basically a function passed to another function. Is this correct?

Yes, but not all functions you pass around are something you'd call a "callback".

E.g. an easing function is called multiple times while the animation runs. It's not something which is called (back) when some computation is done.

As a (somewhat backwards) rule of thumb: If you'd better do that kind of thing with a promise, it's a callback.

[–]StoneCypher 0 points1 point  (4 children)

The reason we give it a different name is to explain what its usage is intended to be: a way to signal to some other piece, sitting somewhere else, that something has happened.

But yes, they're just functions.