No, you cannot make any synchronous io calls, including redis. The only synchronous io calls I know are the file system and console.
However, there are some encoding methods that you can use to make asynchronous encoding more manageable.
- come back earlier by checking the error first.
- move the repeating code into a separate function, for example, create error structures.
- use this asynchronous library: https://github.com/caolan/async . In particular, a waterfall function may be convenient here.
I also think that you will need to pass a callback method to these functions, since they are asynchronous.
- setTokenOnRedis (token);
- deleteTokenOnRedis (token);
I reorganized your sample code, which I hope should be less indented and more readable / supported. I have not used async, I will leave it to you.
Personally, I found that the entire asynchronous node coding model is very frustrating, but you get used to it. After some time, you will learn to use various asynchronous coding patterns, and then it becomes almost valid :)
Some links that may help you:
reorganized code:
module.exports.authenticate = function(request, response){ authenticate(request, response, function(err, reply){ if(err){ reply = authenticationError(err); } response.send(reply); }); }; var authenticationError = function(internalmsg){ return { internalmsg : internalmsg, error : true, code : "AUTH#001", msg : "User authentication failed, Please check user credentials." }; }; var authenticate = function(request, response, callback) { if(UserSchema) { var UserModel, attributes; mongoose.model('user', UserSchema); UserModel = mongoose.model('user'); attributes = request.params; UserModel.findOne(attributes, "_id name email token", function(err, user) { if(err || !user){ return callback(err || "UserModel.findOne, no user"); } var token;