Last active
September 13, 2021 12:52
-
-
Save AnsonH/6cf62ff71fbdcd056587fd9959aced9d to your computer and use it in GitHub Desktop.
Javascript Tip #5 - Sleep
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
/* Tweet: https://twitter.com/AnsonH_/status/1437398568055181314?s=20 */ | |
/* Sleep function */ | |
async function sleep(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
/* Example */ | |
async function takeNap() { | |
console.log("Let me take a sleep 😴"); | |
await sleep(2000); // Sleep for 2 secs. Don't miss the `await` keyword! | |
console.log("I'm awake!"); | |
} | |
takeNap(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related Stack Overflow question