Created
December 4, 2014 22:27
-
-
Save uniphil/76b42c7f8941c5ef9e5b to your computer and use it in GitHub Desktop.
Sync Stamper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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