Home › Forums › JavaScript › jQuery call backs vs chaining. When to use what? › Reply To: jQuery call backs vs chaining. When to use what?
It’s not quite as straightforward a question as you seem to think. It generally has more to do with how the function is written than what it does.
Chaining is when you call a method on an object, the method does something, and then returns the original object. It literally is like a link in a chain.
object--→method--→does something--┐
↑ |
└---returns original object----┘
Callbacks are like (upside-down) trees. You give the function another function, with the expectation that the original function will call your function at some point (often, when done, but this is not a requirement).
function(callback)--→does something
└--→calls callback
jQuery does mainly chaining.
Javascript, in general, is very well-suited to using callbacks.
You may have heard of “promises”, the syntax of which looks like chaining, but is actually a pattern of managing callbacks.