Created
August 31, 2016 23:01
-
-
Save estrattonbailey/90beed697d505a6f612fc72346442d65 to your computer and use it in GitHub Desktop.
Sequencing Delayed Functions
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
/** | |
* Curried delay function | |
*/ | |
const delay = (cb, delay = 0) => (...args) => setTimeout(() => { | |
cb() | |
if (args.length > 0) args.shift()(...args) | |
}, delay) | |
/** | |
* Fire series of delays | |
*/ | |
const queue = (...args) => args.shift()(...args) | |
/** | |
* Create delayed functions | |
*/ | |
const hello = delay( | |
() => console.log('Hello') | |
, 500) | |
const beautiful = delay( | |
() => console.log('beautiful') | |
, 500) | |
const world = delay( | |
() => console.log('world!') | |
, 500) | |
/** | |
* Run sequence | |
*/ | |
queue(hello, beautiful, world) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment