Skip to content

Instantly share code, notes, and snippets.

@giovanniantonaccio
Last active September 20, 2019 17:46
Show Gist options
  • Save giovanniantonaccio/9a581eb83e579a79d2da241b0918dfce to your computer and use it in GitHub Desktop.
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.
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