Semanticize intermediate headings

This commit is contained in:
Chalarangelo
2020-09-29 19:22:26 +03:00
parent 13627134a6
commit 0007611b19
35 changed files with 125 additions and 126 deletions

View File

@ -9,7 +9,7 @@ excerpt: Learn all the different ways you can implement a sleep() function in Ja
JavaScript does not come with a `sleep()` function out of the box and that is probably a good idea considering the environments where it runs and the trouble such a function could cause if used incorrectly. The closest equivalent of such a function is `setTimeout`, however there are other, less common ways to implement a function that will freeze the current thread for a specified amount of time.
**setTimeout**
### setTimeout
JavaScript's `setTimeout` sets a timer which executes a function or specified piece of code once the timer expires. Only the code inside the `setTimeout` callback will execute after the timer expires, which can lead to nesting issues, as well as code executing out of order if you are not careful.
@ -23,7 +23,7 @@ const printNums = () => {
printNums(); // Logs: 1, 3, 2 (2 logs after 500ms)
```
**Synchronous version**
### Synchronous version
While strongly discouraged, `Date.prototype.getTime()` can be used inside a `while` loop to pause execution for a set amount of time. You can easily define a synchronous `sleep()` function like this:
@ -43,7 +43,7 @@ const printNums = () => {
printNums(); // Logs: 1, 2, 3 (2 and 3 log after 500ms)
```
**Asynchronous version**
### Asynchronous version
A less intrusive way to go about implementing a `sleep()` function is to utilize the `async` and `await` keywords added in JavaScript ES6, a `Promise` and `setTimeout()`. Note that the resulting function must be executed in an `async` function and has to be called with `await`: