Asynchronous Javascript: Queue
One of my favorite npm libraries is async by caolan. It provides an impressive collection of utility functions for working with asynchronous javascript.
In this blog series, I will reimplement several of the functions found in async. These implementations will be far simpler (and likely less robust) than caolan’s, but I hope they help illuminate what’s going on under-the-hood.
Queues make for a great place to start.
Our goal is to implement a factory function called queue. queue will create an object with a concurrency limit. Tasks pushed to the queue will be run concurrently up to the specified limit. If the concurrency limit is reached, tasks will be enqueued until space becomes available for them to run.
Let’s get started!
What’s going on here?
Get Griffin Michl’s stories in your inbox
Join Medium for free to get updates from this writer.
First we define a variable to keep track of how many tasks are currently running.
let running = 0Next we create an array to store tasks waiting to be run.
const taskQueue = []Before we look at these two private functions, let’s look at the returned object.
return {
push: task =>
running < concurrency ? runTask(task) : enqueueTask(task),
}The object returned from queue has one method: push. Push has two behaviors. If the number of running tasks is less than the specified concurrency, we can immediately run the pushed task. Otherwise, we need to enqueue the task to be run later. Now let’s see what runTask and enqueueTask do.
runTask takes a task as argument. First it increments the number of running tasks, because we are about to run the passed-in task. Next it invokes the task. The callback handles the ‘cleanup’ once this task completes. When the task finishes, we need to decrement the number of running tasks. Since we are no longer at the limit of our concurrency with the current task finished, we can check if there are any tasks waiting to be run. If there are, we shift the first-in task off the queue and recursively invoke runTask on it.
const runTask = (task) => {
running++
task(() => {
running--
if (taskQueue.length > 0) {
runTask(taskQueue.shift())
}
})
}enqueueTask is pretty straightforward. It simply takes a task and adds it onto the queue.
const enqueueTask = task => taskQueue.push(task)And there you have it. We’ve implemented a basic asynchronous task runner. Below is an example of how this queue will behave with different concurrencies passed in. You can also play with this implementation here: http://jsbin.com/bexejofajo/1/edit?js,console






