Created
July 29, 2016 03:03
-
-
Save oflow/fedb1de6b3d39c570019beb50e8aa894 to your computer and use it in GitHub Desktop.
node-redisでよく使うやつだけ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Redis = require('redis'); | |
function redisConnect() { | |
var client = Redis.createClient(), | |
redis = { | |
end: function() { | |
client.end(); | |
for (let prop in redis) { | |
delete redis[prop]; | |
} | |
} | |
}; | |
['hset', 'hgetall', 'hget', 'hdel'].map(method => { | |
redis[method] = () => { | |
var args = Array.prototype.slice.call(arguments); | |
return new Promise((resolve, reject) => { | |
args.push((error, result) => { | |
return (!error) ? resolve(result) : reject(error); | |
}); | |
try { | |
client[method].apply(client, args); | |
} catch(e) { | |
reject(e); | |
} | |
}); | |
} | |
}); | |
return redis; | |
} | |
var redis = redisConnect(); | |
redis.hgetall('hoge').then(result => { | |
console.log(result); | |
redis.end(); | |
}).catch(error => { | |
console.log(error); | |
redis.end(); | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment