DEV Community

Cover image for Callbacks
GiandoDev
GiandoDev

Posted on

Callbacks

The callback function is a simple function that we pass inside another function as an argument.
Alt Text
The Callback function allows us to write asyncronus code for example the code above is asyncronus code because javascript can wait for a click before running our callback function and move on with the execution of the code.
In synchronous code javascript executes the code from top to bottom, left to right. Without the callback function in the code above, javascript would freeze (blocked) until the user clicks the button.
Now is a good time to talk about event loops and since we are all sitting on the shoulders of the giants, no one better than Philip Roberts explained events loops:
Philip Roberts event loops video
I hope you watched the video;
We may write our callback function also with the arrow function like this:
Alt Text

Top comments (0)