jumar
jumar

Reputation: 309

rewriting Redis calls in Node.js using promises

var Q = require('q');
Q.nfcall(client.get("time_clock", function (err, reply) {
    var time = reply.toString();
    return time;
 })).then(function(time) {
 client.get("time_predicted", function (err, replier) {
    mom=replier.toString();
    res.render('time', {watch: time, moment: mom});
 })
 }).fail(function(err){
   console.log('Error.')})
 .done();
};

This code fails. The code below works, without using promises (shown below). The code I want to fix is above.

client.get("time_clock", function (err, reply) {
    time=reply.toString();
    console.log("in here"+time); // Will print `OK`
client.get("time_predicted", function (err, replier) {
    mom=replier.toString();
    res.render('time', {watch: time, moment: mom});
});
});

What do I need to change in the first code example to make it work? (Note: it would be even better if I could call res.render at the very end, in the last or another 'then').

Upvotes: 0

Views: 91

Answers (1)

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

I won't directly answer to your question; but a simple solution is to use redis-then, a redis library for NodeJS that uses promises.

Upvotes: 1

Related Questions