Skip to content

Instantly share code, notes, and snippets.

@oflow
Created July 29, 2016 03:03
Show Gist options
  • Save oflow/fedb1de6b3d39c570019beb50e8aa894 to your computer and use it in GitHub Desktop.
Save oflow/fedb1de6b3d39c570019beb50e8aa894 to your computer and use it in GitHub Desktop.
node-redisでよく使うやつだけ
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