A Thorough (Re)Introduction to JavaScript Callbacks
Have you came across the term “callback” but don’t know what it means? Don’t worry. You’re not alone. Many newcomers to JavaScript found callbacks hard to understand too.
Zell, a developer from Singapore, wrote a very detailed and explanatory article on the subject. I highly recommend you read it.
This article is aimed at the absolute beginners, but can also serve as a nice refresher for those of us who have been working with JS for a while.
What are callbacks?
A callback is a function that is passed into another function as an argument to be executed later. (Developers say you “call” a function when you execute a function, which is why callbacks are named callbacks).
They’re so common in JavaScript that you probably used callbacks yourself without knowing they’re called callbacks.
Why use callbacks?
Callbacks are used in two different ways — in synchronous functions and asynchronous functions.
Callbacks in synchronous functions
If your code executes in a top to bottom, left to right fashion, sequentially, waiting until one code has finished before the next line begins, your code is synchronous.
Callbacks in asynchronous functions
Asynchronous here means that, if JavaScript needs to wait for something to complete, it will execute the rest of the tasks given to it while waiting.
