Skip to content

Commit

Permalink
Add setTimeout with zero delay (Solves denysdovhan#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
narayanasuri authored Jul 11, 2022
1 parent 7ad136d commit 96cd021
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Currently, there are these translations of **wtfjs**:
- [Split a string by a space](#split-a-string-by-a-space)
- [A stringified string](#a-stringified-string)
- [Non-strict comparison of a number to `true`](#non-strict-comparison-of-a-number-to-true)
- [setTimeout with zero delay](#settimeout-with-zero-delay)
- [πŸ“š Other resources](#-other-resources)
- [🀝 Supporting](#-supporting)
- [πŸŽ“ License](#-license)
Expand Down Expand Up @@ -2212,6 +2213,31 @@ So this comparison is performed like this:

- [**7.2.15** Abstract Equality Comparison](https://262.ecma-international.org/11.0/index.html#sec-abstract-equality-comparison)

## setTimeout with zero delay
```
setTimeout(() => {
console.log("Foo");
}, 0);
console.log("Bar");
```

The output for the above block of code returns:
```
Bar
Foo
```

### πŸ’‘ Explanation:
Although, `setTimeout` is called with `0ms` delay. It still waits for all the lines of code in the document to finish execution before it could print this.

The reason being, setTimeout which is a part of Web APIs maintains its own stack which when popped goes into the callback queue/task queue. JavaScript executes all the lines of a document using the traditional call stack. The callback queue pushes the callback to the call stack only when the call stack is empty (when the entire document has been executed).

**Links:**
- [JSConf EU - What the heck is event loop anyway?](https://www.youtube.com/watch?v=8aGhZQkoFbQ&t=1560s) by Philip Roberts
- [MDN Web Docs for Web APIs](https://developer.mozilla.org/en-US/docs/Web/API)
- [JS Visualizer](https://www.jsv9000.app/)

# πŸ“š Other resources

- [wtfjs.com](http://wtfjs.com/) β€” a collection of those very special irregularities, inconsistencies and just plain painfully unintuitive moments for the language of the web.
Expand Down

0 comments on commit 96cd021

Please sign in to comment.