I have the following action to create a user with node_redis:
server.post('/create_user', function(req, res, next) { console.log(req.body); var body = req.body; client.hincrby('users', 'count', 1, function(err, id) { client.hmset('user:'+id, 'username', body.username, 'password', body.password, 'email', body.email, function(err, write) { client.hmget('user:'+id, 'username', 'email', function(err, read) { res.send({id: id, username: read[0], email: read[1]}); }); }); }); })
I was thinking reading about Deferrable and Promisses here: http://blog.jcoglan.com/2011/03/11/promises-are-the-monad-of-asynchronous-programming/
How can this code be rewritten with Deferrables and Promisses, allowing cleaner exception processing and also better maitenance of the process?
The action are basically:
- Increase counter to get ID
- Set Redis hash of user with ID
- Return created user from Redis