0

Super basic question- how do I know what format I should give my callback for various node_redis calls?

Seems like some examples out there have two arguments (err, res) while others have one (res)

Is there a definitive way to know what the callback should look like?

davidkomer
  • 3,020
  • 2
  • 23
  • 58

1 Answers1

2

The definitive way to know what a callback should look like is to look at a module's source. You can find that here for node-redis, and it is either (err) or (err, res). If you only use (res) you're just going to get the error instead, if there is one.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • You are right in a general sense and thanks for linking to index.js, but I still don't see where it's defined there... say for example "sismember" ? – davidkomer Oct 01 '13 at 04:28
  • Take a look [here](https://github.com/mranney/node_redis/blob/master/index.js#L691) for the internal callback mechanism for all the commands. The command `sismember` also internally uses that mechanism. – hexacyanide Oct 01 '13 at 04:36
  • Yes but that just passes it forward on line 746: command_obj = new Command(command, args, false, buffer_args, callback); command_obj.callback() itself is called in different ways in a few places (see lines 125 vs. 531 for example) and I see no easy way of knowing the exact parameters any given call should expect to eventually process – davidkomer Oct 01 '13 at 06:14
  • The format is consistent with either `(err)` or `(err, res)`, `res` also being `reply` in some cases. What you see at line 125 is an internal error function. It accepts the error `message` and takes the `command_obj` callback, and callbacks `message` as the error. Therefore, it is still `(err)` in that case. – hexacyanide Oct 01 '13 at 23:29