Details on macrotasks and microtasks in JavaScript

Details on macrotasks and microtasks in JavaScript

1. What are microtasks?

Promise

await and async

2. What are the macro tasks?

setTimeout

setInterval

  • DOM Events
  • AJAX Requests

3. Case

<script>

console.log(1)

setTimeout(()=>{

    console.log("2")

},0)

Promise.resolve().then(()=>{

    console.log('3')

})

console.log(4)

</script>

We found that the order of printing is 1-4-3-2

Why is it in this order?

Print 1-4 first. There is definitely no problem.

Why is 3 printed first and then 2?

Because 3 is Promise , Promise is a microtask.

2 is setTimeout , which is a macro task

Microtasks are executed earlier than macrotasks.

So 3 is executed first and then 2

3.1 Conclusion

  • Synchronous first, then asynchronous; micro first, then macro
  • Microtasks are executed earlier than macrotasks.

4. Code Examples

<body>

    <div id="app"></div>

<script>

// This section is DOM rendering let app=document.getElementById("app")

let cont='<p>I am a p tag</p>'

app.append(cont)

// DOM rendering ends console.log(1)

setTimeout(()=>{

    console.log("2")

    alert('setTimeout2')

},0)

Promise.resolve().then(()=>{

    console.log('3')

    alert('3')

})

console.log(4)

</script>

</body>

4.1 Code Analysis

Execution analysis of the above code:

You must execute 1-4 first.

Then according to the micro task first and then the macro task

It outputs 3 and then pops up 3

Then it says output 2 and pops up setTimeout2 [wrong]

Because there is a DOM rendering between microtasks and macrotasks

So then comes dom rendering, and finally the macro task

So after outputting 1-4 , DOM rendering is performed.

Then output 2 and then setTimeout2 pops up

4.2 Conclusion and Application Scenarios

Microtask>DOM rendering>Macrotask See the following example

Application scenarios of this conclusion

We have all done echarts. We know when rendering echarts.

The node can only be rendered after the DOM of the page is rendered.

So some friends will put the request timing in the monitored() life cycle

This ensures that the returned data can be rendered normally on the page.

In fact, based on the above conclusion.

You can request data in create. When I came back.

The DOM is definitely rendered. Because the request is a macro task.

The execution time of the macro task is after DOM rendering.

This is the end of this article about the details of macrotasks and microtasks in JavaScript . For more relevant content about macrotasks and microtasks in JavaScript , please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on macrotasks and microtasks in js
  • JavaScript macrotasks and microtasks
  • A brief discussion on the execution order of JavaScript macrotasks and microtasks
  • A brief discussion on the principles of JavaScript event loop microtasks and macrotask queues
  • Analysis of JavaScript event loop and macro-task and micro-task principles
  • JS event loop mechanism event loop macro task micro task principle analysis
  • JavaScript microtasks and macrotasks explained

<<:  A few front-end practice summaries of Alipay's new homepage

>>:  The problem of introducing specified font @font-face in CSS to be compatible with various browsers

Recommend

HTML table markup tutorial (1): Creating a table

<br />This is a series of tutorials provided...

Detailed explanation of MySQL joint query optimization mechanism

Table of contents MySQL federated query execution...

Introduction to HTML DOM_PowerNode Java Academy

What is DOM? With JavaScript, you can reconstruct...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...

Sample code for partitioning and formatting a disk larger than 20TB on centos6

1. Server environment configuration: 1. Check dis...

WeChat applet implements the Record function

This article shares the specific code for the WeC...

Implementation of mysql data type conversion

1. Problem There is a table as shown below, we ne...

Making a simple game engine with React Native

Table of contents Introduction Get started A brie...

Detailed explanation of Tomcat core components and application architecture

Table of contents What is a web container? The Na...

Detailed explanation of this reference in React

Table of contents cause: go through: 1. Construct...

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...

Linux uses join -a1 to merge two files

To merge the following two files, merge them toge...

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...

Web designer's growth experience

<br />First of all, I have to state that I a...