Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created February 20, 2014 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/9122725 to your computer and use it in GitHub Desktop.
Save kriskowal/9122725 to your computer and use it in GitHub Desktop.
var map = require("collections/map");
function protectAgainstThunderingHerd(method) {
var memo = new Map();
return function protected() {
var self = this;
var args = Array.prototype.slice.call(arguments);
if (!memo.has(args)) {
memo.set(Q.try(function () {
return method.apply(this, args);
}).finally(function () {
memo.delete(args);
}));
}
return memo.get(args);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment