Skip to content

Instantly share code, notes, and snippets.

@kessler
Last active August 29, 2015 14:03
Show Gist options
  • Save kessler/e42dbdb8a6ee2fd811a6 to your computer and use it in GitHub Desktop.
Save kessler/e42dbdb8a6ee2fd811a6 to your computer and use it in GitHub Desktop.
producer consumer example
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