Skip to content

Instantly share code, notes, and snippets.

@lubien
Created March 22, 2017 18:54
Show Gist options
  • Save lubien/e20c0f4e5da5fc621c368b92a190d647 to your computer and use it in GitHub Desktop.
Save lubien/e20c0f4e5da5fc621c368b92a190d647 to your computer and use it in GitHub Desktop.
Execute operations with delay time between them
const Poller = (time, callback) => {
const queue = [];
const add = op => {
queue.push(op)
}
const exec = () => {
callback(queue.shift())
if (queue.length) {
setTimeout(() => exec(), time)
}
}
return {
add, exec
}
}
const poller = Poller(1000, console.log)
poller.add(1)
poller.add(2)
poller.add(3)
poller.exec()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment