Last active
March 22, 2019 21:22
-
-
Save rrfaria/77bd521763e8de4487a50fcb627cf18c to your computer and use it in GitHub Desktop.
Countdown with async await
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
const go = { | |
timer: null, | |
message:'', | |
time:0, | |
countdown: (duration = 10) => { | |
clearInterval(go.timer); | |
return new Promise(function(resolve, reject) { | |
go.timer = setInterval(function() { | |
go.time--; | |
console.log(go.message + ':' + go.time); | |
if (!go.time) { | |
clearInterval(go.timer); | |
resolve(); | |
} | |
}, 1000); | |
}); | |
}, | |
do: async (msg, time=10) => { | |
go.time = time; | |
go.message = msg; | |
await go.countdown(go.time); | |
console.log('fim'); | |
}, | |
} | |
go.do("", 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment