Created
April 23, 2019 15:55
-
-
Save rimiti/214ec4d404a1d9e3bdbed5754f41f3de to your computer and use it in GitHub Desktop.
Loop: running promises in sequential.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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