Skip to content

Instantly share code, notes, and snippets.

@erikthedeveloper
Created May 29, 2016 14:07
Show Gist options
  • Select an option

  • Save erikthedeveloper/3a5de7fc8ba2606340bfc84e4c006cc2 to your computer and use it in GitHub Desktop.

Select an option

Save erikthedeveloper/3a5de7fc8ba2606340bfc84e4c006cc2 to your computer and use it in GitHub Desktop.
A little Javascript challenge
/**
* Given an array of "steps", execute them while accounting for delay
* See: https://medium.com/@kitze/js-coding-challenge-1-test-your-skills-63c2af5446d0#.n2wq6jyqu
* @param {function|int[]} steps
*/
const sequence = (steps) => steps.reduce(
// Reduce the steps, keeping track of an accumulated timeout
(timeout, step) => typeof step === 'function'
// Yes: invoke it with the currently accumulated timeout
? setTimeout(step, timeout) && timeout
// No: it is an integer. Increase accumulated timeout
: timeout + step
, 0);
@erikthedeveloper
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment