JS ES6 asynchronous solution

JS ES6 asynchronous solution

Initially using the callback function

​ Because there was no clear official specification for js at the beginning, there was no clear specification for the parameters in the callback functions passed in the asynchronous functions encapsulated in various third-party libraries, and the meaning of each parameter was not clear, which made it inconvenient to use.

But there are clear specifications in node

Callback mode in node:

1. All callback functions must have two parameters, the first parameter indicates the error, and the second parameter indicates the result

2. All callback functions must be the last parameter of the function

3. All callback functions cannot appear as attributes

es6 asynchronous processing model

After the emergence of Es6, the official proposed specifications for asynchronous processing and a processing model suitable for all asynchronous scenarios. The model has:

  • Two stages: unsettled and settled.
  • Three states: pending, resolved, rejected
  • Always push from the unresolved stage to the resolved stage, and the status of the resolved stage will not change

After the task is in the resolved state, subsequent processing may be required.

  • We call the subsequent processing of resolved thenable
  • We call the subsequent processing of rejected catchable

API tailored for this asynchronous model: promise

How to use promises

Copy

const task = new Promise((resolve, reject) => {     
    // Code for pending task stage // Execute immediately console.log("Start 100-meter long-distance running");  
    setTimeout(() => {  
       if (Math.random() > 0.5) {  
           // Success: Finished // Push to success resolve("finished");  
       } else {  
           // Failure: Broken leg // Push to failure reject("Broken leg");  
       }  
    }, 1000)
});
task.then((result) => {
  console.log(result);
}).catch((error) => {
  console.log(error);
})

After 1 second, the task is pushed to resolved, and subsequent processing is handled in then or catch.

Notice

pending status = > rejected status:

Copy

1. Call reject

2. Code execution error

3. Throwing errors manually

Subsequent processing functions must be asynchronous and will be placed in the micro queue.

After the js execution stack is cleared, the tasks in the micro queue will be executed first. After the tasks in the micro queue are cleared, the tasks in the macro queue will be executed.

  • Macro task queues include: setTimeout, setInterval, setImmediately, I/O, UI render
  • Microtask queues include: promise, process.nexttick, Object.observe (no longer used), Mutation.observe

Async await is the syntactic sugar of promise added in es7. You can also learn about it. This article only gives an overview of promise. There are many other details to master.

The above is the details of JS ES6 asynchronous solution. For more information about ES6 asynchronous solution, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • JS quickly master ES6 class usage
  • Detailed explanation of JS ES6 variable destructuring assignment
  • Several magical uses of JS ES6 spread operator
  • Let's talk about destructuring in JS ES6
  • Implementation of Javascript Destructuring in ES6
  • Usage and difference between let and const in ES6 specification in JavaScript
  • 24 ES6 methods to solve practical JS development problems (summary)
  • Detailed explanation of the implementation principle of JavaScript ES6 Class
  • Specific use of ES6 Proxy in JavaScript
  • Detailed explanation of JS ES6 coding standards

<<:  Detailed explanation of 7 SSH command usages in Linux that you don’t know

>>:  InnoDB type MySql restore table structure and data

Recommend

How to use Navicat to export and import mysql database

MySql is a data source we use frequently. It is v...

Detailed Tutorial on Installing VirtualBox 6.0 on CentOS 8 / RHEL 8

VirtualBox is a free and open source virtualizati...

Analysis of 2 Token Reasons and Sample Code in Web Project Development

Table of contents question: There are 2 tokens in...

How to install JDK 13 in Linux environment using compressed package

What is JDK? Well, if you don't know this que...

Detailed explanation of transactions and indexes in MySQL database

Table of contents 1. Affairs: Four major characte...

How to understand SELinux under Linux

Table of contents 1. Introduction to SELinux 2. B...

Detailed explanation of Truncate usage in MySQL

Preface: When we want to clear a table, we often ...

Summary of special processing statements of MySQL SQL statements (must read)

1. Update the entire table. If the value of a col...

Implementing Markdown rendering in Vue single-page application

When rendering Markdown before, I used the previe...

Proxy_pass method in multiple if in nginx location

1. First, let's review the relevant knowledge...

Detailed explanation of the execution process of JavaScript engine V8

Table of contents 1. V8 Source 2. V8 Service Targ...

How to configure the same domain name for the front and back ends of nginx

This article mainly introduces the method of conf...