DEV Community

loading...

Understanding callback functions

henryfradley profile image Henry Fradley ・1 min read

I have had some issues with comprehending the purpose of callback functions and their usage in my journey of learning programming. In this post I will be explaining callbacks and their usefulness.

So essentially a callback function is just a function that is passed as an argument to a different function with the intent that it will be 'called back' at a later time.

Callbacks are especially useful for asynchronous functions, an asynchronous function we may be waiting on a request to send back data and we do not know how long this will take. Callbacks are perfect for that scenario because using a callback you can wait until the request is completed before this data is sent or used by the function!

For example callbacks are often used when retrieving data from an API or from a database. Using callback functions allows you to keep performing other actions while the data is still coming in!

Good luck with your callbacks and be sure to stay out of callback hell which is an entire different story!

Discussion (0)

pic
Editor guide