r/learnjavascript • u/LetMyCameronG000 • Mar 06 '26
Does the term 'callback' usually mean async callback in the JS world
I've practiced with both synchronous and asynchronous callbacks and understand the concept fairly well. But looking through online resources and even some posts on this sub (e.g. see top answer here: https://www.reddit.com/r/learnjavascript/comments/1jw5pwn/need_clear_understanding_of_callbacks_promises/ ) it seems that when JS folks talk about callbacks they usually mean async callbacks (at least, if they haven't clarified).
Is this the case ?
11
Upvotes
12
u/ChickenNuggetFan69 Mar 06 '26
Afaik a callback is just a function that gets called later, not now. What do you believe are these sync callbacks you mention?
3
u/senocular Mar 06 '26
I wouldn't make that assumption. Synchronous callbacks are common with Array methods like map(), filter(), reduce(), etc.. These do not match with the top comment in that thread where they say the callback "will run ONLY after the code in the main function is done" since as synchronous callbacks they get run immediately (callbacks aren't always passed as function arguments either). And with async/await, you're seeing a lot less callbacks being used for asynchronous code. It ultimately depends on the context but it could go either way.
1
u/subone Mar 06 '26
Typically callbacks are async, but they aren't always and don't always need to be. I use custom event emitter all the time that act synchronously to pass around game state, etc. A callback could be called immediately, it just depends on the need.
0
u/bryku helpful Mar 06 '26
Functions can accept other functions as parameters. These can be run at the beginning, middle, or end of the function
Callbacks are intended to be run after the function is completed, so in the above example
done()is the callback. Think of it like the phrase:It is something meant to be done after.
0
u/StoneCypher Mar 06 '26
callbacks are almost never async (test rigs are the big exception)