Skip to content

Instantly share code, notes, and snippets.

@n370
Last active July 23, 2018 18:59
Show Gist options
  • Save n370/3f5877a7226dfad7730b4fb25e8e4eb0 to your computer and use it in GitHub Desktop.
Save n370/3f5877a7226dfad7730b4fb25e8e4eb0 to your computer and use it in GitHub Desktop.
A naive example on how to schedule the execution in series of a list of async tasks.
const a = [];
a.push(() => setTimeout(() => { console.log('a'); a.shift(); a.length && a[0]();}, 1000))
a.push(() => setTimeout(() => { console.log('b'); a.shift(); a.length && a[0]();}, 1000))
a.push(() => setTimeout(() => { console.log('c'); a.shift(); a.length && a[0]();}, 1000))
a.push(() => setTimeout(() => { console.log('d'); a.shift(); a.length && a[0]();}, 1000))
a.push(() => setTimeout(() => { console.log('e'); a.shift(); a.length && a[0]();}, 1000))
a[0]();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment