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

How to set up URL link in Nginx server

For websites with an architecture like LNMP, they...

How to Easily Remove Source Installed Packages in Linux

Step 1: Install Stow In this example, we are usin...

Mycli is a must-have tool for MySQL command line enthusiasts

mycli MyCLI is a command line interface for MySQL...

Various methods to restart Mysql under CentOS (recommended)

1. MySQL installed via rpm package service mysqld...

Two ways to create SSH server aliases in Linux

Preface If you frequently access many different r...

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query....

How to use VUE and Canvas to implement a Thunder Fighter typing game

Today we are going to implement a Thunder Fighter...

Description of the execution mechanisms of static pages and dynamic pages

1. A static page means that there are only HTML ta...

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, yo...

CSS achieves the effect of aligning multiple elements at both ends in a box

The arrangement layout of aligning the two ends o...

CSS beginner tutorial: background image fills the entire screen

If you want the entire interface to have a backgr...

Detailed explanation of the solution to image deformation under flex layout

Flex layout is a commonly used layout method nowa...