Created
March 22, 2017 18:54
-
-
Save lubien/e20c0f4e5da5fc621c368b92a190d647 to your computer and use it in GitHub Desktop.
Execute operations with delay time between them
This file contains 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
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