You might get another question regarding the Sinclair's jealous group, for example, right in the synchronous
function of which executes Colbeck after finishing, it's a synchronous task.
And actually, if you don't know in JavaScript, where using callbacks quite a lot and previously before
promises and disintegrate, we used callback for fetch and data.
Also even now, callback is quite popular way to work inside the application.
So actually, first of all, we want to write some function which will make some asynchronous code.
And the easiest way to simulate in JavaScript asynchronous code is by using set timeout because, for
example, were fetching some data from the backend and it takes time.
This is why I want to create here a sink function, and they just name it as a fan.
And here we want to get our callback as a parameter and actually callback is just a function.
But the most important part is that the synchronous function doesn't know what callback is.
It can be whatever because we're calling it from outside.
Now inside we are doing some asynchronous code, for example, would fetch data from a page or in our
case, here we're using set timeout to simulate this.
And we have set timeout, for example, for two seconds.
And then at the end, we want to call our callback.
This is why we simply call this function or sometimes with the data as the result of our API.
Maybe.
So here we can write code back done.
And this is it.
Our asynchronous function is ready.
Now how are using this function?
We just tried to sync function and we are providing our callback and said this is a function which will
be executed inside at the end.
This is why here we can see that we are getting the function with the argument message.
This is what we are getting back from our async function.
In here we can write console.log, callback and here comma message.
And let's check if it's working.
I'm reloading the page.
And as you can see, we don't hear anything now after two seconds were getting called back done.
This is exactly how callbacks are working and this is exactly what we implemented.
So we have in the synchronous function, which executes callback after finishing, it's a synchronous
task.
And the second question that you might get here, what problem callback solve.
And actually, there are two important points.
First of all, Colback, allow us to make some assume control staff and wait for the result because
actually here we're just providing the function from outside.
And it will be called later and not immediately.
This is the main purpose of the callback.
And secondly, it's important to remember that inside our asynchronous function of we don't know what
this callback.
This is why we can build shareable things.
This callback can do whatever in different cases.
For example, on the one page, we want to fetch data and maybe render this data, and on another page,
we want to fetch this data and calculate the total number of posts or something like this, which actually
means callback is a generic function.
This is why we can easily share our asynchronous function without specific implementation of our callback.