diff --git a/README.md b/README.md index 88d89840c..8b9c31e13 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ * [Scroll to top](#scroll-to-top) * [Shuffle array values](#shuffle-array-values) * [Similarity between arrays](#similarity-between-arrays) +* [Sleep](#sleep) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) @@ -667,6 +668,21 @@ const similarity = (arr, values) => arr.filter(v => values.includes(v)); // similarity([1,2,3], [1,2,4]) -> [1,2] ``` +### Sleep + +Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`. + +```js +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +/* +async function sleepyWork() { + console.log('I\'m going to sleep for 1 second.'); + await sleep(1000); + console.log('I woke up after 1 second.'); +} +*/ +``` + ### Sort characters in string (alphabetical) Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`. diff --git a/snippets/sleep.md b/snippets/sleep.md index c64ed3917..e2a58d0bd 100644 --- a/snippets/sleep.md +++ b/snippets/sleep.md @@ -4,9 +4,11 @@ Delay executing part of an `async` function, by putting it to sleep, returning a ```js const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -// async function sleepyWork() { -// console.log('I\'m going to sleep for 1 second.'); -// await sleep(1000); -// console.log('I woke up after 1 second.'); -// } +/* +async function sleepyWork() { + console.log('I\'m going to sleep for 1 second.'); + await sleep(1000); + console.log('I woke up after 1 second.'); +} +*/ ```