DEV Community

Cover image for Callbacks
GiandoDev
GiandoDev

Posted on

3

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay