Execute a chain of functions

CoffeeScript JavaScript

Often the very first lines of code executed in an application involve a series of chunks of instructions that get things started and don’t respond to changes of context or user input. The instructions simply need to execute in order.

You can always just call one function but the callbacks can get a little crazy and lead to duplicated code to handle different cases. For me that code breaks up easily into a series of functions that handle different concerns.

This one liner is a little code golf, but it’s been useful for me.

Line 1 says we have a function called execute that takes functions (an array of functions) as an argument. Line 5 executes this function that we just declared supplying arrayOfFunctions. I’ve placed the if statement on line 2 here for clarify, that’s different from the one liner where the if statement comes after the code that does the work. Line 2 says if there are any functions left, get the first element and execute it, passing it an anonymous function. This anonymous function simply calls execute recursively, supplying it with our array of functions.

Note: this is indeed recursion. So it won’t execute more than a few thousand functions before you get a call stack overflow. Other than that you should be golden.

Also note: Arrays and Objects are passed by reference in JavaScript, so we get the array after it’s been shifted, since it’s called somewhere in the function that you just passed to it. What function you say? Yes, let’s take a look at those.

Each one of your functions has only one requirement: calling the callback in it’s arguments when it’s work is done. When all the functions have been executed, the execute function checks the length of the array and stops executing.

It’s simple, it’s effective and it makes chains of functions easy to queue up and control with a little callback technique.

Comments Off



New tool available for validating JavaScript objects. Bake freely into your design-by-contract recipes.

Solicitor on GitHub

Comments Off

Git-fu: List all commit messages from the last two weeks


Sometimes you want to see all of your commit messages based on time. This is one way to do it.

Which will output this

This example uses --since, you can also use --after, --until/--before, as well as formatted dates like --since="2011-06-1"

Editing in place with Backbone.js views

Backbone.js CoffeeScript JavaScript

Backbone.js is incredibly agnostic when it comes to…well…almost everything. You can persist your models any way you like at any time. You can use templates or not. You can bind Views to DOM objects or you can make a view render a DOM fragment and append it later.

The problem

With so many options you eventually end up writing a lot of boilerplate just to get things started. At that point, we start to abstract the common useful elements from our classes into a super class we can inherit from. This is the result of one such abstraction.

One solution


With EditView, you can allow your users to quickly edit your model’s attributes and minimize overhead.

Continue reading

WordFinder 9000

CoffeeScript JavaScript

I don’t often get the chance or pleasure to use software to help my wife. One day while she was doing literacy work in Haiti, she presented me with a problem that seemed like my cup of tea.

The problem

We have a list of letters that should be known to students at various times throughout the year: through first grade, second grade, etc.

We also have a list of words from children’s books that contain more or less age appropriate words in stories.

What we need is a list of age appropriate words that are possible with the letters they’re learning.

— my wife

In addition to being really useful (think of the kids), the problem was interesting enough to keep me awake a little longer and boring enough for me to make it really interesting. Thus began the Word Finder 9000.

Continue reading

Success and error callbacks


I’ve heard some developers [who?] complain that writing error handlers isn’t ideal.

You can imagine if the success handler contains more logic and goes on to call other asynchronous functions it gets a bit hairy. But what if we simply started putting the error handler first? That would be a bit of a drag.

It’s not much in terms of work involved but it does take a process that handles errors as more than just a simple non-success.

Another way to go about it would be to define a default ajaxError handler via $.ajaxError(handlerFunction).

Create dynamic key javascript object with one line

CoffeeScript JavaScript

How many times have you done this?

man failing to use measuring tape

Continue reading

“Cut” and Paste files in Mac OSX finder



Do this instead.

Copy with cmdc.
Paste with optioncmdv

Continue reading

Constant 403 error with nginx on osx



Despite checking permissions, nginx will return nothing but a 403 error page.


Added this line


I installed nginx on my mac like everyone else using this guide, and it worked for my site until I changed the configuration to a different directory a second time. Something was different.

I started to get 403 errors, so I checked the logs and didn’t find anything more helpful than this.

After about 2 hours burned changing permissions on everything imaginable I found this guy, who figured out /usr/local/etc/nginx/nginx.conf, needs the user line to read

It was that simple!