Last active
August 29, 2015 14:03
-
-
Save kessler/e42dbdb8a6ee2fd811a6 to your computer and use it in GitHub Desktop.
producer consumer example
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 work = [] | |
| function consume() { | |
| // consume at most 1 item everu tick | |
| // this is not always optimal... | |
| var item = work.pop() | |
| if (item) | |
| consumeImpl(item) | |
| setImmediate(consume) | |
| } | |
| function produce() { | |
| console.log('producing work...') | |
| work.push('work item') | |
| // push work every second | |
| setTimeout(produce, 1000) | |
| } | |
| function consumeImpl(item) { | |
| console.log('consuming %s', item) | |
| // do something with item | |
| } | |
| produce() | |
| consume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment