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

HTML uses the title attribute to display text when the mouse hovers

Copy code The code is as follows: <a href=# ti...

Nginx+FastDFS to build an image server

Installation Environment Centos Environment Depen...

Example of how to set up a multi-column equal height layout with CSS

Initially, multiple columns have different conten...

MySQL v5.7.18 decompression version installation detailed tutorial

Download MySQL https://dev.mysql.com/downloads/my...

Detailed examples of ajax usage in js and jQuery

Table of contents Native JS How to send a get req...

Zabbix WEB monitoring implementation process diagram

Take zabbix's own WEB interface as an example...

img usemap attribute China map link

HTML img tag: defines an image to be introduced in...

mysql8.0.11 winx64 manual installation and configuration tutorial

First of all, let me talk to you about my daily l...

Perfect solution to Docker Alpine image time zone problem

Recently, when I was using Docker to deploy a Jav...

How to package the uniapp project as a desktop application

Installing Electron cnpm install electron -g Inst...

RHEL7.5 mysql 8.0.11 installation tutorial

This article records the installation tutorial of...

WeChat applet to achieve the revolving lantern effect example

Preface In daily development, we often encounter ...