3

I've run into an issue with NodeJS where, due to some middleware, I need to directly return a value which requires knowing the last modified time of a file. Obviously the correct way would be to do

getFilename: function(filename, next) {
    fs.stat(filename, function(err, stats) {
        // Do error checking, etc...
        next('', filename + '?' + new Date(stats.mtime).getTime());
    });
}

however, due to the middleware I am using, getFilename must return a value, so I am doing:

getFilename: function(filename) {
    stats = fs.statSync(filename);
    return filename + '?' + new Date(stats.mtime).getTime());
}

I don't completely understand the nature of the NodeJS event loop, so what I was wondering is if statSync had any special sauce in it that somehow pumped the event loop (or whatever it is called in node, the stack of instructions waiting to be performed) while the filenode information was loading or is it really blocking and that this code is going to cause performance nightmares down the road and I should rewrite the middleware I am using to use a callback? If it does have special sauce to allow for the event loop to continue while it is waiting on the disk, is that available anywhere else (though some promise library or something)?

1
  • It's strange that the middleware does not allow for async execution because without IO you cannot get anything done. Commented Nov 29, 2012 at 18:33

1 Answer 1

2

Nope, there is no magic here. If you block in the middle of the function, everything is blocked.

If performance becomes an issue, I think your only option is to rewrite that part of the middleware, or get creative with how it is used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.