Skip to content

Commit

Permalink
Merge pull request #138 from kawanet/fix-set-promise
Browse files Browse the repository at this point in the history
fix: .set() method without options
  • Loading branch information
alevy authored Dec 16, 2020
2 parents 9976627 + 7492d40 commit 3155b9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Client.prototype.get = function(key, callback) {
Client.prototype.set = function(key, value, options, callback) {
if(callback === undefined && typeof options !== 'function') {
var self = this;
if (!options) options = {};
return promisify(function(callback) { self.set(key, value, options, function(err, success) { callback(err, success); }); });
}
var logger = this.options.logger;
Expand Down Expand Up @@ -238,6 +239,7 @@ Client.prototype.set = function(key, value, options, callback) {
Client.prototype.add = function(key, value, options, callback) {
if(callback === undefined && options !== 'function') {
var self = this;
if (!options) options = {};
return promisify(function(callback) { self.add(key, value, options, function(err, success) { callback(err, success); }); });
}
var logger = this.options.logger;
Expand Down Expand Up @@ -296,6 +298,7 @@ Client.prototype.add = function(key, value, options, callback) {
Client.prototype.replace = function(key, value, options, callback) {
if(callback === undefined && options !== 'function') {
var self = this;
if (!options) options = {};
return promisify(function(callback) { self.replace(key, value, options, function(err, success) { callback(err, success); }); });
}
var logger = this.options.logger;
Expand Down Expand Up @@ -392,6 +395,7 @@ Client.prototype.increment = function(key, amount, options, callback) {
if(callback === undefined && options !== 'function') {
var self = this;
return promisify(function(callback) {
if (!options) options = {};
self.increment(key, amount, options, function(err, success, value) {
callback(err, {success: success, value: value});
});
Expand Down
19 changes: 19 additions & 0 deletions test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ test('SetSuccessfulWithoutOption', function(t) {
});
});

test('SetPromiseWithoutOption', function(t) {
var n = 0;
var dummyServer = new MemJS.Server();
dummyServer.write = function(requestBuf) {
var request = MemJS.Utils.parseMessage(requestBuf);
t.equal('hello', request.key.toString());
t.equal('world', request.val.toString());
n += 1;
dummyServer.respond({header: {status: 0, opaque: request.header.opaque}});
};

var client = new MemJS.Client([dummyServer]);
return client.set('hello', 'world').then(function(val) {
t.equal(true, val);
t.equal(1, n, 'Ensure set is called');
t.end();
});
});

test('SetWithExpiration', function(t) {
var n = 0;
var dummyServer = new MemJS.Server();
Expand Down

0 comments on commit 3155b9a

Please sign in to comment.