It’s a common misconception that callbacks are asynchronous.
It is not.
Let’s examine why.
What’s a Callback?
Callback is an important JavaScript concept you have to understand.
And they’re super simple: They’re just functions that are passed into another function as an argument.
Here’s an example of a simple callback in action.
// `log` is the callback in this exampleconst items = [1, 2, 3]items.forEach(log)
function log(item) { console.log(item)}In this case:
logis just a function.- By itself,
logis not a callback. - When it is passed into
forEach,logbecomes a callback.
That’s it.
A more accurate way to define callbacks
The above definition is workable but not accurate.
There’s a more accurate way to define callbacks. Learning this will help you understand why and how we use callbacks.
In this definition: callbacks are accepted into a function as an argument. These functions are then called within the receiving function.
This requires a bit of explanation so let’s consider the following code:
// fn is the callbackfunction greet (fn) { const message = "hello world" fn(message)}
function sayName (message) { console.log(message + " my name is Zell Liew")}
greet(sayMyName)) // Hello world, my name is Zell LiewIn this example:
greettakes in a function (fn) as an argument.- This
fnis a callback. - The
fnis called somewhere ingreet.
Practically both definition work because the callback (either fn or sayName) refer to the same value.
But theoretically, you can see how this deeper definition gives you a better understanding of how and why callbacks are used.
Now let’s move on.
Most callbacks are synchronous
Both callbacks in the above examples are synchronous because they do not wait for anything.
In the forEach example, JavaScript executes each log function without waiting for any events.
const items = [1, 2, 3]items.forEach(log)
function log(item) { console.log(item)}Likewise, in the detailed (greet and sayMyName) example, JavaScript goes logs the sentence without waiting for any events.
function greet (fn) { const message = "hello world" fn(message)}
function sayName (message) { console.log(message + " my name is Zell Liew")}
greet(sayMyName)) // Hello world, my name is Zell LiewOnly a handful of callbacks are asynchronous
Here’s a fun fact: We cannot make asynchronous callbacks from scratch.
All asynchronous callbacks must be built on top of a function that is already asynchronous to begin with.
Here are some examples:
addEventListenersetTimeoutsetIntervalXMLHttpRequestfetchIntersectionObserver
That’s all you need to know about callbacks and the difference between synchronicity and asynchronicity.
For the rest of this course, we will focus on giving you the practical knowledge you need to be able to use Asynchronous JavaScript and Rest APIs fluently.
For this to happen, you need to understand what an API is.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson