Last active
September 20, 2019 17:46
-
-
Save giovanniantonaccio/9a581eb83e579a79d2da241b0918dfce to your computer and use it in GitHub Desktop.
This gist simulates the execution of two async methods using promises. It returns a message of success when both complete. A random timeout between 1 and 3000ms is being used.
This file contains 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
function randomTimeout() { | |
const t = Math.floor(Math.random() * 3000 + 1); | |
return t; | |
} | |
const method1 = new Promise(resolve => { | |
timeout = randomTimeout(); | |
setTimeout(() => resolve("method1"), timeout); | |
}); | |
const method2 = new Promise(resolve => { | |
timeout = randomTimeout(); | |
setTimeout(() => resolve("method2"), timeout); | |
}); | |
Promise.all([ | |
method1.then(result => console.log(result)), | |
method2.then(result => console.log(result)) | |
]).then(() => console.log("Success")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment