Skip to content

Instantly share code, notes, and snippets.

@flockonus
Last active October 1, 2018 21:45
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 flockonus/1c173ef70d0702ac96f29e0961f007fb to your computer and use it in GitHub Desktop.
Save flockonus/1c173ef70d0702ac96f29e0961f007fb to your computer and use it in GitHub Desktop.
Example of concurrency using Javascript promises + await
const sleep = (seconds) => {
return new Promise((accept) =>
setTimeout(accept, seconds * 1000, `slept ${seconds}`),
);
};
async function execParallel() {
console.log(new Date(), 'starting');
// promises will execute concurrently, so that execution time = slowest one (not the sum)
const out = await Promise.all([sleep(5), sleep(5), sleep(5)]);
console.log(new Date(), 'ended:', out);
}
execParallel().then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment