Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,38 @@ AsyncRedis.createClient = (...args) => new AsyncRedis(args);
const commandsToSkipSet = new Set(['multi']);
// this is the set of commands to promisify
const commandSet = new Set(commands.filter(c => !commandsToSkipSet.has(c)));
// this is the set of commands that return a Multi
const queueCommandSet = new Set(['batch', 'multi']);
// this is the set of Multi commands to promisify
const multiCommandSet = new Set(['exec', 'exec_atomic']);

const promisify = function (object, method) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to use utils.promisify now too: https://nodejs.org/dist/latest-v10.x/docs/api/util.html#util_util_promisify_original

(It's since v8 of node)

return (...args) => new Promise((resolve, reject) => {
args.push((error, ...results) => {
if (error) {
reject(error, ...results);
} else {
resolve(...results);
}
});
method.apply(object, args);
});
}

AsyncRedis.decorate = redisClient => objectDecorator(redisClient, (name, method) => {
if (commandSet.has(name)) {
return (...args) => new Promise((resolve, reject) => {
args.push((error, ...results) => {
if (error) {
reject(error, ...results);
} else {
resolve(...results);
return promisify(redisClient, method);
} else if (queueCommandSet.has(name)) {
return (...args) => {
// Decorate the Multi object
const multi = method.apply(redisClient, args);
return objectDecorator(multi, (multiName, multiMethod) => {
if (multiCommandSet.has(multiName)) {
return promisify(multi, multiMethod);
}
return multiMethod;
});
method.apply(redisClient, args);
});
}
}
return method;
});
Expand Down