Skip to content

Instantly share code, notes, and snippets.

@kironroy
Last active August 28, 2023 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kironroy/5d82f3190c94ae1dc288be97ecdd57d5 to your computer and use it in GitHub Desktop.
Save kironroy/5d82f3190c94ae1dc288be97ecdd57d5 to your computer and use it in GitHub Desktop.
'use strict;
const lotteryPromise = new Promise(function (resolve, reject) {
console.log('lottery draw is happening 🔮');
setTimeout(function () {
if (Math.random() >= 0.5) {
resolve('You WIN 💰');
} else {
reject(new Error('You lost your money 😔'));
}
}, 2000);
});
lotteryPromise.then(res => console.log(res)).catch(err => console.error(err));
// Promisifying setTimeout
const wait = seconds => {
return new Promise(resolve => {
setTimeout(resolve, seconds * 1000);
});
};
wait(2)
.then(() => {
console.log('I waited for 2 seconds');
return wait(1);
})
.then(() => console.log('I waited for 1 second'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment