N
N
NkDev2020-01-26 10:48:25
Programming
NkDev, 2020-01-26 10:48:25

What is a callback in programming?

What are callbacks?
What is their meaning and why are they needed?
Why are they called that?
I only know that this is a function that is passed as an argument to another function.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Sokolov, 2020-01-26
@NkDev

A callback is another function that will be called “later”. A household analogue is when you send an empty envelope
with your own address and stamps in a letter to the radio station so that they send an answer in it. This is usually necessary for asynchronous execution - when lines of code are not sequentially executed one after another, but some long task is started (and released) and it is not yet known when it will end. For example, you need to make an HTTP request to a remote server and get a response from it. Then, not only the request parameters are passed to the function that will deal with communication with the distant server, but also the callback function that will be called after receiving the response, with the data from the response.

G
Griboks, 2020-01-26
@Griboks

When a function is running for a long time, there is no point in waiting for it to complete (visually it will look like a hung browser and the user will close the site). Therefore, a callback is passed to her, they say, you will inform yourself when you finish the work.

A
Andrey Dobrin, 2020-01-26
@andreydobrin

It's very simple:
You correctly said that this is a function that is passed as an argument to another function:

function  first(callbak){
----здесь пишем код функции----
---- здесь можно записать код "функции обратного вызова" 
и она сработает тогда, когда выполнится функция first()----
}

In simple terms, there is one function and it has another in its parameter. The function itself will be executed first, and then the one that is the argument of this function.

A
Alexander Skusnov, 2020-01-26
@AlexSku

I applied in Direct Show (video processing). There is a third-party stream that passes frames through itself. The main program can constantly interrogate him if a new frame has appeared. But why waste time on surveys when they will only rarely return a positive result? Therefore, the main program simply passes a link to its handler (callback function), and the thread that skips the video will always call it at the right time (it is desirable that the handler has time to do everything quickly).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question