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 modelAfter the emergence of Es6, the official proposed specifications for asynchronous processing and a processing model suitable for all asynchronous scenarios. The model has:
After the task is in the resolved state, subsequent processing may be required.
API tailored for this asynchronous model: promiseHow 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
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.
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:
|
<<: Detailed explanation of 7 SSH command usages in Linux that you don’t know
>>: InnoDB type MySql restore table structure and data
Optimize the fastcgi configuration file fcgiext.i...
MySql is a data source we use frequently. It is v...
VirtualBox is a free and open source virtualizati...
To beautify the table, you can set different bord...
Origin: A few days ago, a tester sent a requireme...
Table of contents question: There are 2 tokens in...
What is JDK? Well, if you don't know this que...
Table of contents 1. Affairs: Four major characte...
Table of contents 1. Introduction to SELinux 2. B...
Preface: When we want to clear a table, we often ...
1. Update the entire table. If the value of a col...
When rendering Markdown before, I used the previe...
1. First, let's review the relevant knowledge...
Table of contents 1. V8 Source 2. V8 Service Targ...
This article mainly introduces the method of conf...