Skip to content

Instantly share code, notes, and snippets.

@rimiti
Created April 23, 2019 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rimiti/214ec4d404a1d9e3bdbed5754f41f3de to your computer and use it in GitHub Desktop.
Save rimiti/214ec4d404a1d9e3bdbed5754f41f3de to your computer and use it in GitHub Desktop.
Loop: running promises in sequential.
/**
* @description Runs getUser() in sequential.
* @return {Promise<void>}
*/
async function example() {
for (let i = 0; i < 10; i++) {
await getUser();
}
}
/**
* @description Runs getUser() in parallel.
* @return {Promise<void>}
*/
async function example() {
[1,2,3,4,5,6,7,8,9].forEach(async () => {
await getUser();
})
}
/**
* @description Simulates a HTTP request.
* @return {Promise<any>}
*/
const getUser = () => new Promise((resolve) => {
setTimeout(() => {
console.log('Current date:', new Date());
resolve();
}, 4000);
});
example()
.then(() => console.log('done.'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment