Last active
January 3, 2016 06:09
-
-
Save watson/8420602 to your computer and use it in GitHub Desktop.
Thinking about the most simple queueing implementation...
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 process = function (a, b) { | |
| // do some async stuff with a and b | |
| }; | |
| process(foo1, bar1); | |
| process(foo2, bar2); |
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 queue = []; | |
| var processNext = function () { | |
| var a = queue[0][0], | |
| b = queue[0][1]; | |
| // do some async stuff with a and b | |
| queue.shift(); | |
| if (queue.length > 0) | |
| process.nextTick(processNext); | |
| }; | |
| var prepare = function () { | |
| queue.push(arguments); | |
| if (queue.length === 1) | |
| process.nextTick(processNext); | |
| }; | |
| prepare(foo1, bar1); | |
| prepare(foo2, bar2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment