Skip to content

Instantly share code, notes, and snippets.

@rrfaria
Last active March 22, 2019 21:22
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 rrfaria/77bd521763e8de4487a50fcb627cf18c to your computer and use it in GitHub Desktop.
Save rrfaria/77bd521763e8de4487a50fcb627cf18c to your computer and use it in GitHub Desktop.
Countdown with async await
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