diff --git a/lib/callback_filler.js b/lib/callback_filler.js index 8bd874c..a1ae7f2 100644 --- a/lib/callback_filler.js +++ b/lib/callback_filler.js @@ -6,9 +6,11 @@ CallbackFiller.prototype.fill = function(key, err, data) { var waiting = this.queues[key]; delete this.queues[key]; - waiting.forEach(function(task) { - (task.cb)(err, data); - }); + if (waiting && waiting.length) { + waiting.forEach(function(task) { + (task.cb)(err, data); + }); + } }; CallbackFiller.prototype.has = function(key) { diff --git a/test/caching.unit.js b/test/caching.unit.js index 348506f..223f221 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -515,6 +515,19 @@ describe("caching", function() { }); }); + context("when callback called twice by client", function() { + it("it does not throw an error", function(done) { + cache.wrap(key, function(cb) { + methods.getWidget(name, cb); + methods.getWidget(name, cb); + }, opts, function(err, widget) { + checkErr(err); + assert.deepEqual(widget, {name: name}); + setTimeout(done, 10); + }); + }); + }); + it("lets us make nested calls", function(done) { function getCachedWidget(name, cb) { cache.wrap(key, function(cacheCb) {