Skip to content

Instantly share code, notes, and snippets.

@gngenius02
Created March 2, 2020 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gngenius02/2d2ff248da94c21a296e82d952364140 to your computer and use it in GitHub Desktop.
Save gngenius02/2d2ff248da94c21a296e82d952364140 to your computer and use it in GitHub Desktop.
//delay fn
const delay = (time) => new Promise((resolve) => setTimeout(resolve, parseInt(time * 1000)));
//random fn
const random = () => Math.random() * 1.5;
const iterationArray = new Array(10);
const runFn = async (rounds) => {
let i = -1
for await (round of rounds) {
const time = random()
await delay(time);
rounds[++i] = parseFloat(time)
console.log(`${i} rounds completed.`)
}
return rounds;
}
runFn(iterationArray).then((respFromRun) => {
const reduced = respFromRun.reduce((p, c) => p + c, 0);
console.log({ respFromRun, reduced });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment