Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active December 6, 2019 22:46
Show Gist options
  • Save davidgilbertson/9d440d27da7f6c60a714fa027136580b to your computer and use it in GitHub Desktop.
Save davidgilbertson/9d440d27da7f6c60a714fa027136580b to your computer and use it in GitHub Desktop.
console.clear();
(async () => {
const list = ['one', 'two', 'three'];
const doSlowly = msg => new Promise(resolve => {
setTimeout(() => {
resolve(`Processed ${msg}`);
}, 100);
});
console.time('Processed sequentially in');
const serialResults = [];
for (const listItem of list) {
const result = await doSlowly(listItem);
serialResults.push(result);
}
console.timeEnd('Processed sequentially in');
console.log('Serial results:', serialResults);
console.time('Processed in parallel in');
const parallelResults = await Promise.all(list.map(doSlowly));
console.timeEnd('Processed in parallel in');
console.log('Parallel results:', parallelResults);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment