Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hongphuc5497/2ab17fbf80874fd0b9da099200c086b0 to your computer and use it in GitHub Desktop.
Save hongphuc5497/2ab17fbf80874fd0b9da099200c086b0 to your computer and use it in GitHub Desktop.
const SamplePromise = (index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Promise ${index}`);
}, 1000);
});
};
const withoutPromiseAll = () => {
console.time('Without Promise.all');
[...Array(10).keys()].map(async (index) => {
await SamplePromise(index);
});
console.timeEnd('Without Promise.all');
};
withoutPromiseAll();
const withPromiseAll = () => {
console.time('With Promise.all');
Promise.all([...Array(10).keys()].map(async (index) => {
await SamplePromise(index);
}));
console.timeEnd('With Promise.all');
};
withPromiseAll();
const withPromiseAllSettled = () => {
console.time('With Promise.allSettled');
Promise.allSettled([...Array(10).keys()].map(async (index) => {
await SamplePromise(index);
}));
console.timeEnd('With Promise.allSettled');
};
withPromiseAllSettled();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment