Created
January 19, 2019 13:19
-
-
Save kimaldis/aeadff19a673633712b5392b6313fa2d to your computer and use it in GitHub Desktop.
Javascript Sleep Function
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
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
// useage, await sleep(2000): | |
// | |
async function demo() { | |
console.log('Taking a break...'); | |
await sleep(2000); | |
console.log('Two second later'); | |
} | |
// or: | |
// | |
sleep(500).then(() => { | |
// Do something after the sleep! | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment