luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

Express / Redis / Wait For #17

Closed ngetha closed 9 years ago

ngetha commented 10 years ago

Hi

I need to send a command to redis and wait for the response while responding to a request in Express.

Code Below - Error is 'Error Error: ERR wrong number of arguments for 'get' command'

var isPresent = wait.for(client.get.bind(client), sid, function(err, resp){
        console.log("ugw::redis:: <- b %s %s", err, resp);
        return resp;
    });
luciotato commented 10 years ago

try

var resp = wait.forMethod(client,"get", sid);

console.log(resp);

inancgumus commented 10 years ago

This doesn't work, why?

var $r = require('heroku-redis-client').createClient(),
    $wait = require('wait.for');

module.exports = function(id) {
  var n = parseInt($wait.for($r.hget, 'counter:item', id));
  return n || 0;

Gives this error:

TypeError: Object #<Object> has no method 'send_command'
    at RedisClient.(anonymous function) (/Development/node_modules/heroku-redis-client/node_modules/redis/index.js:959:25)
    at Object.Wait.applyAndWait (/Development/node_modules/wait.for/waitfor.js:45:12)
    at Object.Wait [as for] (/Development/node_modules/wait.for/waitfor.js:61:21)
    at Array.module.exports [as 0] (/Development/my.js...)
inancgumus commented 10 years ago

Oh, I am sorry. After some testing I've figured it out.

Correct code:

var $r = require('heroku-redis-client').createClient(),
    $wait = require('wait.for');

module.exports = function(id) {
  var n = parseInt($wait.forMethod($r, 'hget', 'counter:item', id));
  return n || 0;