1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Add optional callback option to duplicate function

This commit is contained in:
Ruben Bridgewater
2016-04-13 04:00:23 +02:00
parent d2b8f2f391
commit 8e24380d53
3 changed files with 52 additions and 2 deletions

View File

@@ -79,7 +79,11 @@ RedisClient.prototype.unref = function () {
}
};
RedisClient.prototype.duplicate = function (options) {
RedisClient.prototype.duplicate = function (options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var existing_options = utils.clone(this.options);
options = utils.clone(options);
for (var elem in options) { // jshint ignore: line
@@ -87,5 +91,18 @@ RedisClient.prototype.duplicate = function (options) {
}
var client = new RedisClient(existing_options);
client.selected_db = this.selected_db;
if (typeof callback === 'function') {
var ready_listener = function () {
callback(null, client);
client.removeAllListeners(error_listener);
};
var error_listener = function (err) {
callback(err);
client.end(true);
};
client.once('ready', ready_listener);
client.once('error', error_listener);
return;
}
return client;
};