Skip to content

Instantly share code, notes, and snippets.

@uniphil
Created December 4, 2014 22:27
Show Gist options
  • Select an option

  • Save uniphil/76b42c7f8941c5ef9e5b to your computer and use it in GitHub Desktop.

Select an option

Save uniphil/76b42c7f8941c5ef9e5b to your computer and use it in GitHub Desktop.
Sync Stamper
var stamper = function(queue) {
var i = 0,
waiting = false;
function incr() {
i += 1;
waiting = false;
}
function incrAfter() {
if (!waiting) {
queue(incr);
waiting = true;
}
}
return function() {
incrAfter();
return i;
};
};
// eg. for node
var stamp = stamper(process.nextTick);
stamp(); // => 0
stamp(); // => 0
process.nextTick(function() {
stamp(); // => 1
});
process.nextTick(function() {
stamp(); // => 1 nooooooooo
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment