Skip to content

Instantly share code, notes, and snippets.

@tobiash
Created June 6, 2012 20:30
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiash/2884566 to your computer and use it in GitHub Desktop.
Save tobiash/2884566 to your computer and use it in GitHub Desktop.
Q + node_redis
# Applies Q.nbind to all redis operations and returns a wrapped client
_ = require("underscore")._
Q = require "q"
nbindOps = (client) ->
functions = _.functions client
# All the ops are available as upper/lowercase functions, I exploit this
# to filter out the Redis operations from the other functions of the client
#
ops = functions.filter (f) -> f.toUpperCase() is f
lc = (op.toLowerCase() for op in ops)
ops = ops.concat(lc)
p = {}
for op in ops
p[op] = Q.nbind client[op], client
p["multi"] = p["MULTI"] = ->
m = client.multi.call client, arguments
m.exec = Q.nbind m.exec, m
m
p
nbind = (client) ->
client.q = nbindOps(client)
client
module.exports =
nbindOps: nbindOps
nbind: nbind
@topliceanu
Copy link

Nice hack on figuring out which methods are redis commands 💎

@goalsin
Copy link

goalsin commented Apr 22, 2014

why not use nfcall ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment