Sitemap
Griffin Michl

Griffin Michl

Asynchronous Javascript: Map

2 min readMay 18, 2016

Our goal here is to implement a version of map that will work with asynchronous functions. Map will apply the asynchronous iteratee function to each element in the array and return an array of results. While these async functions may resolve in any order, we want to maintain the order of our original collection in the results array.

Because map itself is asynchronous, we will give map its own callback. Once every function has resolved, we pass the complete results array to map’s callback. If an error occurs on any element, that error will be passed up to map’s callback and the function will terminate. We will assume all callbacks are in the node error-first style.

Our simple test function is implemented using setTimeout. It doubles first argument when the timeout finishes and passes it to a node-style callback.

Now lets take a look at one implementation of an asynchronous map.

Get Griffin Michl’s stories in your inbox

Join Medium for free to get updates from this writer.

First thing’s first, we need to create a results array and a variable to keep track of how many functions are still running. By keeping track of how many functions are running, we’ll know when we’re done mapping.

let running = collection.length
const results = []

Next, we iterate over the collection and apply the iteratee function to each value along with a callback.

collection.forEach((value, index) => 
iteratee(value, (err, data) => {

When the callback is invoked, there are two possible results. If the iteratee function throws an error, we want to immediately pass that error to map’s callback and terminate the function.

if (err) {
callback(err)
return
}

Otherwise, we need to update the current state of map. We add add the result of the iteratee function to results array at the same index as in the collection.

results[index] = data

This point may be a bit tricky.

First, how do we still have access to `index` when this callback could be called after we’ve already finished iterating through the array? The answer is closures. Functions defined inside functions retain access to the environment in which they were defined. So index inside the callback is the same value as index when the callback was created.

Second, what if we want to add something to the 2nd index of an empty array? I wasn’t totally sure what would happen when I first wrote this, so I tested it in node.

It works just as I hoped. Javascript is smart enough to add `undefined` to each missing element up to the added one and update the length accordingly.

Now that we’re convinced that adding to the results index will work, we need to decrement the number of running functions. If, after decrementing, no functions are left running, we pass the results array to map’s callback.

running--
if (running === 0) {
callback(null, results)
}

And that’s it!

If you have any feedback on these posts or ideas for future topics, tweet me @griffinmichl.

--

--