Skip to content

Instantly share code, notes, and snippets.

@estrattonbailey
Created August 31, 2016 23:01
Show Gist options
  • Save estrattonbailey/90beed697d505a6f612fc72346442d65 to your computer and use it in GitHub Desktop.
Save estrattonbailey/90beed697d505a6f612fc72346442d65 to your computer and use it in GitHub Desktop.
Sequencing Delayed Functions
/**
* 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