Callbacks…. A love story

Ry Hull
3 min readNov 4, 2020

Functions LOVE to function. Functions love to function so much that sometimes a function will function within another function. One could call it a function conjunction. Or an injunction of a plethora of functions.

What might sound confusing is actually really useful. Let us break it down.

A function is a block of code that is executed when it is called later by the function name. When a function is called values can be passed through it. These are called parameters or arguments and can be any number of values and can be named anything. If one chooses to use more than one parameter or argument then they will be passed in the order of which they are listed. The number of parameters or arguments must be the same number as the values listed in the function for it to run properly.

Now we are at the point where we can pass a function into another function. This is applied by placing the function into one of the parameters into the other function.

Passing one function through another is very common and has a multitude of uses in Javascript. Because functions are objects in Javascript(almost everything is an object in Javascript), the function is treated like an object when it is pulled through the other function. This function that is treated like an object when it is pulled through another function is called a callback.

Here is a simple form of a callback. The aMarvelousFunction above operates like a function and has the character of an object. The aSensationalFunction calls the aMarvelousFunction in the last row of the code above and the placeholder ‘callback’ is used as the second value as well. A ‘cb’ is sometimes also used instead of callback. Let’s take a quick look at an anonymous function and some code with multiple callback functions.

The first function is considered anonymous because the callback function is defined inside the aStoopendousFunction when it is called. This is incredibly useful when the code starts getting more complex. Next we see a function with multiple callbacks. A function can hold as many callbacks as the imagination can render so take as many swings as it takes when implementing them. Call back functions are also useful as a synchronous function or an asynchronous function.

--

--