Skip to content

Instantly share code, notes, and snippets.

@AnsonH
Last active September 13, 2021 12:52
Show Gist options
  • Save AnsonH/6cf62ff71fbdcd056587fd9959aced9d to your computer and use it in GitHub Desktop.
Save AnsonH/6cf62ff71fbdcd056587fd9959aced9d to your computer and use it in GitHub Desktop.
Javascript Tip #5 - Sleep
/* 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();
@AnsonH
Copy link
Author

AnsonH commented Sep 13, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment