diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 88c235d..4444bf4 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -64,7 +64,11 @@ var multiCaching = function(caches, options) { if (_isCacheableValue(result)) { // break out of async loop. - return cb(err, result, i); + if (typeof cb === 'function') { + return cb(err, result, i); + } else { + return cb.resolve(result); + } } i += 1; @@ -72,7 +76,17 @@ var multiCaching = function(caches, options) { }; cache.store.get(key, options, callback); - }, cb); + }, function(err, result) { + if (typeof cb === 'object') { + if (err) { + cb.reject(err); + } else { + cb.resolve(result); + } + } else if (typeof cb === 'function') { + cb(err, result); + } + }); } function setInMultipleCaches(caches, opts, cb) { @@ -85,7 +99,17 @@ var multiCaching = function(caches, options) { } else { next(); } - }, cb); + }, function(err, result) { + if (typeof cb === 'object') { + if (err) { + cb.reject(err); + } else { + cb.resolve(result); + } + } else if (typeof cb === 'function') { + cb(err, result); + } + }); } /** @@ -98,10 +122,18 @@ var multiCaching = function(caches, options) { self.getAndPassUp = function(key, cb) { getFromHighestPriorityCache(key, function(err, result, index) { if (err) { - return cb(err); + if (typeof cb === 'function') { + return cb(err); + } else { + return cb.reject(err); + } } - cb(err, result); + if (typeof cb === 'function') { + cb(err, result); + } else { + cb.resolve(result); + } if (index) { var cachesToUpdate = caches.slice(0, index); @@ -227,7 +259,11 @@ var multiCaching = function(caches, options) { options: options }; - setInMultipleCaches(caches, opts, cb); + var defer = Promise.defer(); + + setInMultipleCaches(caches, opts, cb || defer); + + return defer.promise; }; /** @@ -246,7 +282,11 @@ var multiCaching = function(caches, options) { options = {}; } - getFromHighestPriorityCache(key, options, cb); + var defer = Promise.defer(); + + getFromHighestPriorityCache(key, options, cb || defer); + + return defer.promise; }; /** diff --git a/lib/stores/memory.js b/lib/stores/memory.js index 0d6d4f9..7480382 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -27,6 +27,8 @@ var memoryStore = function(args) { lruCache.set(key, value, maxAge); if (cb) { process.nextTick(cb); + } else { + return Promise.resolve(value); } }; @@ -35,6 +37,7 @@ var memoryStore = function(args) { cb = options; } var value = lruCache.get(key); + if (cb) { process.nextTick(function() { cb(null, value); diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 09da385..c50247e 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -181,6 +181,19 @@ describe("multiCaching", function() { }); }); }); + + describe('using promises', function() { + it('should return a promise and resolve it', function(done) { + memoryCache3.set(key, value) + .then(function() { + return multiCache.get(key); + }) + .then(function(result) { + assert.equal(result, value); + }) + .then(done); + }); + }); }); describe("del()", function() {