Skip to content

Instantly share code, notes, and snippets.

@JayMGurav
Last active July 15, 2021 02:47
Show Gist options
  • Save JayMGurav/60a4329d5989085ce3e74b46c7ac4405 to your computer and use it in GitHub Desktop.
Save JayMGurav/60a4329d5989085ce3e74b46c7ac4405 to your computer and use it in GitHub Desktop.
serialize random timers to print numbers from 0-10 in order
function randomTime() {
const min = 0, max = 6;
return (Math.floor(Math.random() * (max - min + 1)) + min) * 1000;;
}
Array.from({length:10}, (_, k) => k+1).reduce(async (prev, curr) => {
await prev;
return new Promise((resolve) => {
setTimeout(resolve,randomTime(), console.log(curr))
});
}, Promise.resolve(console.log(0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment