JavaScript macrotasks and microtasks

JavaScript macrotasks and microtasks

Macrotasks and Microtasks

  • JavaScript is a single-threaded language (DOM will go crazy if it is multi-threaded)
  • Therefore, only one task can be executed at a time , called the main thread, which is used to perform synchronous tasks.
  • There are also two task lists for storing asynchronous tasks, macro tasks and micro tasks.
  • The execution order is: main thread => microtask => macrotask

About timer

  1. The timer module puts it into the macro task queue when the time point arrives
  2. If the main thread has no tasks, it will execute them. If it has tasks, it will wait until the execution is completed before continuing.
  3. If there are two timers with the same time, the upper one will be executed first and the lower one will be executed later.
  4. If two timers have different times, the shorter timer will be executed first and the longer timer will be executed later.

Note:

  • The timer's ⏲ is completed in the timer module, and after completion it is the same as a normal asynchronous task.
  • In terms of time, due to the long time of the main thread practice, there may be a delay


insert image description here

About Promises

  • The constructor of promise is a synchronous task
  • The execution order is always: synchronization => microtask => macrotask
  • In nested code, there may be synchronization, macrotasks, and microtasks in macrotasks. In this case, put them in the queue/main thread of the next execution to wait for execution.
setTimeout(() => {
    console.log("timer");
    setTimeout(() => {
      console.log("timeout timeout");
    }, 0);
    new Promise(resolve => {
      console.log("settimeout Promise");
      resolve();
    }).then(() => {
      console.log("settimeout then");
    });
  }, 0);
  new Promise(resolve => {
    console.log("Promise");
    resolve();
  }).then(() => {
    console.log("then");
  });
  console.log("ssss");

Execution order: Promise=>ssss=>then=>timer=>settimeout Promise=>settimeout then=>timeout timeout

DOM rendering tasks

Browser rendering: CSS+DOM execution encounters JS and JS is executed first
You can put js as much as possible below: Avoid white screen

Task shared memory

Tasks will not be executed simultaneously, but will be scheduled one by one. They share memory.

Promise microtasks handle complex business

Using promises can turn tasks into asynchronous tasks so that they do not affect the execution of synchronous tasks.

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

You may also be interested in:
  • Details on macrotasks and microtasks in JavaScript
  • A brief discussion on macrotasks and microtasks in js
  • 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

<<:  Summary of basic usage of $ symbol in Linux

>>:  Installation and configuration of mysql 8.0.15 under Centos7

Recommend

Centos7 install mysql5.6.29 shell script

This article shares the shell script of mysql5.6....

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

Examples and comparison of 3 methods for deduplication of JS object arrays

Table of contents 1. Comparison of data before an...

How to optimize MySQL deduplication operation to the extreme

Table of contents 1. Clever use of indexes and va...

A brief introduction to MySQL storage engine

1. MySql Architecture Before introducing the stor...

How to migrate mysql storage location to a new disk

1. Prepare a new disk and format it with the same...

In-depth explanation of Mysql deadlock viewing and deadlock removal

Preface I encountered a Mysql deadlock problem so...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...

Docker implements container port binding local port

Today, I encountered a small problem that after s...

MySQL MyISAM default storage engine implementation principle

By default, the MyISAM table will generate three ...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

How to get USB scanner data using js

This article shares the specific process of js ob...