Jim Drannbauer home resume

How to do it with node.js

01 Feb 2011

Given:

var x = {
      something: 'cool'
    },
    y = {
      something: 'uncool'
    };

var doTheCommonThing = function(thing) {
  console.log('did the common thing');
}

var doSomething = function(thing) {
  console.log('did something');
}

var doSomethingElse = function(thing) {
  console.log('did something else');
}

Here’s something you might see in node/javascript code:

function doIt(thing) {
  doTheCommonThing(thing);
  if (thing.something === 'cool') {
    doSomething(thing);
  } else {
    doSomethingElse(thing);
  }
}

doIt(x);
doIt(y);

Do it like this instead:

function doItRight(thing, callback) {
  doTheCommonThing(thing);
  callback(thing);
}

doItRight(x, doSomething);
doItRight(y, doSomethingElse);

Get used to it. It’s the future.

Micah and I paired on this post.

blog comments powered by Disqus