Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const state = {
counter: 0
};
const randomInt = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
const sleep = (min, max = min) =>
new Promise(resolve => {
setTimeout(resolve, randomInt(min, max));
});
const incrementCounter = () => {
const currentCounter = state.counter;
sleep(1000, 2000).then(() => {
state.counter = currentCounter + 1;
});
};
incrementCounter();
incrementCounter();
sleep(3000).then(() => console.log(state.counter)); // aléatoirement 1 ou 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment