asynchronous JavaScript static function variable

I have a problem with a "static" function in javascrip (nodejs server).

User.create = function(data, _callback){        
    var node = db.createNode(data);
    var _user = new User(node);
    _user.save(function(err){
        if(err) return callback(err, null);
        _user.index(function(err){
            if(err) return callback(err, null);
            callback(null, _user);  
        })
    })
};

If I call this function twice the _user variable in the internal callback function takes the new value, it seems it overrides the function var instead of allocate a new one.

I need calling this function to allocate a new variable, so it waits save and index functions to complete without changing _user variable.

JavaScript variables are indeed function scoped, so there wouldn't be any explanation for var _user not defining a new variable on subsequent runs.

Looking at the code, I would be more suspicious of what's happening in your User constructor - perhaps it contains some scoping or other logical issues resulting in identical users being created on subsequent calls. Similar "suspects" would be the data parameter getting passed in, as well as db.createNode(). Only suggesting these areas, because it's more likely that there's a programmatic issue at play, rather than JavaScript not following the rules :)

Also, I noticed that your User.create function accepts a parameter called _callback, but later on is invoking callback. I don't know if that's a typo in your example, or if you're accidentally invoking a callback from a higher scope not shown in the example, but that could produce weird behavior.