Asynchronous JavaScript using Callbacks

I recently struggled when faced with a problem of using callbacks to handle the asynchronous flow of multiple API calls.

So I figured I’d do a deep dive into callbacks to refresh and solidify my knowledge.  After all, if you want to truly understand something, teach it to someone else!

Functions as First Class Objects

Before we start talking about callbacks, we must first know that JavaScript functions are first-class objects.

Which means what?  And why is that important to a discussion about callbacks?

Being a first-class object in JavaScript means that functions can have properties and methods just like any other object.  Consider this:

It also means that functions can be assigned to a variable.

More importantly for our discussion on callbacks, however, is that because functions are first-class objects, they can be passed to (and returned from) other functions.

This last point, passing functions as arguments to other functions, is the foundation of callbacks.

A callback is simply a function that is passed to another function as an argument, that can then be used (executed) inside that other function.

The previous example used a named function (answer) as the callback. You can (and probably have) also use anonymous functions as callbacks.  This is common when passing callbacks to the native prototype methods for arrays, objects and strings.

Consider the following:

Here we used an anonymous arrow function as our callback to output twice the value of each element.

Now that we know what callbacks are, let’s talk about their use in asynchronous JavaScript.

Understanding ASYNCHRONY

Image of the Police Album - Synchronicity

No, this isn’t what we’re talking about, but the word ‘asynchony’ makes me think of this album…..  Yeah, I know, I’m old…

Before we start talking about controlling asynchronous flow we must first understand the nature of the JavaScript event loop and the fact that it is single threaded. Digging deep into the JavaScript engine is beyond the scope of this post, but understanding the single threaded nature of the event loop is critical to understanding why we need asynchronous code.

For now, think of the event loop as a queue (first in, first out) that executes snippets of your JS code one by one and in the order they were placed in the queue.

Now think of an Ajax request that we might use to get some data from an API.  That Ajax request is costly (in time).  If we place it in the event loop and run it synchronously it will block everything that comes after it while it waits for a response. This affects performance and causes people to leave your site!

In his You Don’t Know JS series, Kyle Simpson describes asynchrony as the difference between now and later, as opposed to parallel, which describes two processes that are executed independent of each other (but possibly at the same time).

This now and later concept is where callback functions come in.  Rather than running that Ajax request from start to finish, blocking all other code execution while we wait, we pass it a callback.

In this scenario, the JS engine can make the Ajax call (now), continue with the event loop, and once the Ajax call returns (later) pass the callback, along with any data it needs from the response, back into the event loop.

Async in action

Let use an example to show what we’re talking about.  For the remaining examples, I’m going to use setTimeout() to simulate async functions, but you can think of it as time spent making an Ajax request.

This example shows asynchronous control of the event loop. Let’s dig deeper in case this isn’t clear.

We’ve defined three functions first, second and delay.

first and second merely log “first” and “second” to the console so we can tell when they’re executed.

delay takes a callback, which it passes to setTimeout(). setTimeout() sets a timer, then calls the function it was passed – in this case, it will call callback after 500 milliseconds.

After the three function definitions, we call delay(first), which puts the function delay() in our event loop.  Upon execution, delay sets a timer for 500 ms, after which, it will call its callback (in this case first()).

Because the timer in setTimeout() is non-blocking, or asynchronous, the event loop is now free to move on to the next snippet of JS, which is our function call to second().

When second is executed, it logs “second” to the console, which is why we see “second” logged to the console before we see “first”, even though delay(first) was called before second().

And finally, after the 500 ms timer elapses, first() is placed in our event loop and executed, which is when “first” shows up in our console.

I’m hoping the example above was simple enough that you could figure it out on your own, otherwise, maybe my long winded explanation helped.

Fun with callbacks and closure

OK, now that we’re pros with callbacks, lets have a little fun with them!

We’re going to use callbacks and closure to set up a four lane race. Granted, it’s only a race in the sense that we’re randomizing the setTimout() time, but it’s fun nonetheless…

Hopefully, the comments in the code clear up how the “race” is being run. If you have any questions, feel free to leave them below.








FCC Advanced Algorithm Scripting Challenge – ‘Symmetric Difference’

Let’s tackle another Free Code Camp advanced algorithm – ‘Symmetric Difference’.  The challenge here is to ‘create a function that takes two or more arrays and returns an array of the symmetric difference ( or ) of the provided arrays.’

The symmetric difference of two sets of elements is the set that occurs in either one of the two sets, but not both (i.e. the symmetric difference of [1,2,3,4] and [3,4,5,6] is [1,2,5,6]).  For more reading on symmetric difference, check out this wiki page.

Free Code camp gives us the following as a starting point:

This is another case where we have to write a function for which we need to handle an unknown number of arguments.  To accomplish this, we’ll be using the arguments object.  I’ve covered the arguments object in a previous post, ‘Seek and Destroy’.  If you are unfamiliar with or would like a refresher of the arguments object, please check it out.

One of the helpful hints FCC provides us with is a link to the Array.prototype.reduce() method.  The arr.reduce method is a great way to cycle through and apply a function to each element of an array.

The arr.reduce method takes 2 arguments, a callback function (which will be applied to each element of the array) and an initial value (the element to provide as the first argument to the first callback function call – see here for more).

If you’re new to JavaScript, or somewhat unclear of what a callback function is, has an excellent post on callbacks.

The callback takes a possibility of 4 arguments – previousValue, currentValue, currentIndex and array.  This is important, as it means when we write our callback function, we will have access to these 4 values within the function (for further clarity on exactly what the four values are, check out the MDN link mentioned previously).

I think we should break down this challenge into two main problems.  We need to:

  • Write a function that returns an array of the symmetric difference (⊕) between two given arrays
  • Use this function to handle an unknown number of arguments. For example:
    • find (arg1 ⊕ arg2)
    • if there’s an agr3, find arg3 ⊕ (arg1 ⊕ arg2)
    • continue for argX

At this point, I’m going to start discussing my solution to the problem.  If you’re working through FCC, I’d suggest attempting a solution on your own given what we’ve discussed above. Otherwise, proceed on…


Let’s start by writing the function we’ll use to calculate the symmetric difference between two arrays.

In other words, we need to write a function that receives two arrays as arguments, then returns an array of the numbers that are in one of the argument arrays, but not both.

One way to do this is to

  • create a new empty array (curDiff) to store the symmetric diff
  • use arr.reduce() to cycle through each array (need to compare each array against the other, not just one against the other)
  • in the callback function of arr.reduce
    • check if an element is in the other array or in the curDiff array (to prevent duplicates in curDiff)
    • if not – push element to curDiff
      • hint – arr.indexOf() will return -1 if the given element is not found
  • return curDiff

Here’s the JavaScript code:

We now have a function (findSymDiff()) that we can use within our main function to get the symmetric difference between two arrays.  We now have to set up the logic to handle an unknown number of parameters received by our main function.

Our logic (using the arguments object) can look something like this:

  • create new array (symDiff) to store the array returned by findSymDiff()
  • compare arguments[0] to arguments[1] using findSymDiff()
  • cycle through argument[]
    • compare symDiff to arguments[x]

To accomplish this, we can set up a simple for loop.

If we set

we can call

in our for loop starting at i=1 and loop while i<arguments.length.

Then all we have to do is return symDiff!

Here’s how everything looks when we put it together:

This solution passes all of the FCC test cases.

Thanks for reading, feel free to leave comments or additional solutions below.  And if you’re working through Free Code Camp, hopefully this helped straighten out the code.



FCC ‘Seek and Destroy’

This post will work through ‘Seek and Destroy’, one of freeCodeCamp‘s basic algorithm scripting challenges.  This challenge will help us introduce the Arguments object and callback functions.

The Seek and Destroy challenge states that ‘provided with an initial array (the first argument in the destroyer function), followed by one or more arguments’ we need to write a function that will remove the values of all the arguments given after the initial array, from the initial array.

Sounds confusing, I know…  Let’s visualize it to clear things up.


One of the major problems we have lies with ‘followed by one or more arguments’…  If we don’t know how many arguments there will be, how do we write a function to handle them?

The answer to that comes in the form of the Arguments object.  JavaScript provides a local variable ‘arguments’ that acts like an array with an element for each argument passed to the function.  You access the arguments just like you would an element of any other array like so:

The arguments object makes solving this algorithm challenge much easier.  Looking forward at ways to solve this problem, we need to a) access the arguments we need to delete from the initial array and b) somehow remove those arguments from the array.

Storing arguments for easy access

The first thing we’ll do is create a new array and store all the arguments (other than the initial array) in it.

As you can see, arguments can be treated like any other array…  Here, we used the length property and looped through arguments (starting at element 1 so as to avoid the initial array, which will be passed in as the first argument, or arguments[0]).  Setting it up this way will handle any number of arguments passed to the destroyer function.

The push() method in JavaScript is used to ‘push’ new elements on to the end of the calling array.

Tools we’ll need to remove arguments from the array

In order to remove the arguments from the initial array, we will use the filter() method.  The filter method introduces a concept we haven’t talked about here at CrookedCode: the callback function.

A Quick note on functions in JavaScript

The power of functions, and what they allow you to do, are core to successful JavaScript programming.  Functions can be passed to and returned from other functions.  They can also be stored as variables.

A function that accepts another function as an argument is called a higher-order function, whereas, the function that is passed as the argument is called a callback function.  The Eloquent JavaScript chapter on functions is a great tutorial on the use of functions in JavaScript.

Eloquent JavaScript also devotes an entire chapter to higher-order functions.  I highly recommend reading and working through the examples in both of these chapters.  If you’re looking for something a little quicker, has an excellent article on understanding callbacks and higher order functions.

The filter() method accepts a callback function and performs that callback on every element in the calling array.  If the callback returns true for a given element, that element is kept.  If the callback returns false, obviously, that element is not kept.

As stated above, filter() calls the callback function on every element of the calling array, but it does not mutate that array.  Therefore, you must save the kept elements as a new array.

The one final tool we’ll need before we try to solve this algorithm is the array.indexOf() method.  indexOf() will return the first index at which a given value is found.  If the given value is not found, indexOf() returns -1.

Putting it all together…. (finally)

We’ve already created argArr[] to hold the values that we need to delete from the initial array.  We now need to write a callback function for filter() that will return true if we want the element to stay and false if we want to delete that element.

We know that indexOf() will return -1 if a given value is not found.  We also know that we want to keep elements of the initial array that aren’t found in argArr[].  So, for each element of the initial array, lets call argArr.indexOf(), and return true for values of -1.  Therefore, we’ll be returning true for (and keeping) values that aren’t in argArr[].

Like so:

Wow…  This post covered a bunch of stuff we haven’t previously gone over here at CrookedCode, hopefully this helped straighten out the code for you.  See you next time…