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

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️