Member-only story

JavaScript Interview Questions

The Execution Order of Asynchronous Functions in the Event Loop

Shuai Li
4 min readSep 21, 2021

Interview Question

What is the output of the following code?

Analysis

This question is actually examining the execution order of the code. To be precise, it is examining the difference between asynchronous functions such as setTimeout, Promise, and async/await.

Let’s look at the simplest case first:

console.log('111')console.log('222')

What is the execution result of this code? Anyone who has studied programming knows that the program will output 111 and 222 in sequence. There is no asynchronous code in this code, so it is very simple.

Let’s look at another example:

console.log('111')setTimeout(() => {
console.log('333')
}, 0)
console.log('222')

--

--

Shuai Li
Shuai Li

Written by Shuai Li

An enthusiastic game player of Earth Online.

Responses (3)