Problem

In what order will the logs below show up in the console?

console.log('First')

setTimeout(function () {
  console.log('Second')
}, 0)

new Promise(function (res) {
  res('Third')
}).then(console.log)

console.log('Fourth')
Solution

In what order will the logs below show up in the console?

console.log('First')

setTimeout(function () {
  console.log('Second')
}, 0)

new Promise(function (res) {
  res('Third')
}).then(console.log)

console.log('Fourth')

First, Fourth, Third, Second.

To fully understand this one you need an understanding of JavaScript’s event loop – specifically, the Call Stack, Task Queue, Web APIs (for setTimeout), and the Job Queue. Turns out, that’s a little much to cover here so do some Googling and uh GLHF.