What is the use of a synchronous callback function in JavaScript?
Some very common examples of synchroneous callbacks are the methods on Array.prototype
: forEach
, map
, filter
, etc.
The role of the callback is to provide a partial implementation that can be easily swapped in a larger algorithm. Some design patterns like template method and strategy come to mind.
You're right.
There is usually no reason to have a sync callback.
Exceptions include callbacks that might sometimes be async, or callbacks that can be raised more than once (eg, [].map()
).