Question: I'm using Redis to generate IDs for my in memory stored models. The Redis client requires a callback to the `INCR` command, which means the code looks like

```
client.incr('foo', function(err, id) {
  ... continue on here
});

```

The problem is, that I already have written the other part of the app, that expects the `incr` call to be synchronous and just return the ID, so that I can use it like

```
var id = client.incr('foo');

```

The reason why I got to this problem is that up until now, I was generating the IDs just in memory with a simple closure counter function, like

```
var counter = (function() {
  var count = 0;
  return function() {
    return ++count;
  }
})();

```

to simplify the testing and just general setup.

Does this mean that my app is flawed by design and I need to rewrite it to expect callback on generating IDs? Or is there any simple way to just synchronize the call?

Answer: 
It is a bit of a pain, but what you have to do is wrap the logic that you had after the counter was generated into a function, and call that from the Redis callback. If you had something like this:

```
var id = get_synchronous_id();
processIdSomehow(id);

```

you'll need to do something like this.

```
var runIdLogic = function(id){
  processIdSomehow(id);
}

client.incr('foo', function(err, id) {
  runIdLogic(id);
});

```

You'll need the appropriate error checking, but something like that should work for you.

There are a couple of sequential programming layers for Node (such as [TameJS](http://tamejs.org/)) that might help with what you want, but those generally do recompilation or things like that: you'll have to decide how comfortable you are with that if you want to use them.
The application state in node.js is normally passed around as an object. What I would do is closer to:

```
var state = {}

client.incr('foo', function(err, id) {
  state.id = id;
  doSomethingWithId(state.id);
});

function doSomethingWithId(id) {
  // reuse state if necessary
}

```

It's just a different way of doing things.