I always read and hear this term in many places, but it is not very clear to me what it refers to when talking about a "callback". I don't know if it's an anonymous function or something.
I always read and hear this term in many places, but it is not very clear to me what it refers to when talking about a "callback". I don't know if it's an anonymous function or something.
A callback is a function. You give this function as a parameter to another function so that when it finishes executing it calls your callback.
Normally your callback function does something with the result of the function it was passed to. That way you don't have to wait for the function you're calling to finish to continue doing other things, because you know it's going to "call you back" when it has the results.
To put it crudely, you tell a function:
A callback can be any function, anonymous functions or lambdas are usually used but not necessary.
Callback function example when using jQuery:
When the Ajax is done, it calls the callback function
mostrar()
.A callback is a function that will be executed inside another function.
In JS this is an example:
Callbacks are used when doing asynchronous operations , that is, code executions that are performed in a "thread" type, and when that execution ends, its result can be obtained with a callback.
Complementing the answer of the other users, I like to use the visual interpretation that a "calculator" type program offers.
You have, in your calculator, lots of buttons. From the most basic with elementary operations, to those that operate with complex numbers. All of them can do operations of all kinds, and I will assume that you know how to use a calculator.
+
=
Well, this apparently basic example is a visual example of what a callback is. When you press the button
=
, that button executes a procedure, which determines which operation to perform on which numbers. This procedure is called "callback", which we can crudely translate as "function called by this procedure".This idea is very useful when you work with user interfaces ( GUI ) and menus.