Skip to content

Instantly share code, notes, and snippets.

@rimiti rimiti/index.js

Created Apr 23, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.