JS asynchronous Task Processing Mode 2

Source: Internet
Author: User

First, before ES6: using the original callback function, you will fall back to the region two, ES6:

1. Promise

 //Click the OK button to get a list of denominations Getfaceresult () {this.getlocation (this.ph                            Onenum). Then (res = = (Res.status = = && res.data.success) {                            Let province = Res.data.obj.province;                            Let city = res.data.obj.city; This.getfacelist (province, city). Then (res = = {if (re                                    S.status = = = && res.data.success) {this.facelist = Res.data.obj                    }                                })                        }                    }) . catch (Err = {Console.log (ERR)})}  

Promise's approach solves the callback hell, but this approach is full of Promise methods, and if the process is then() complicated, the entire code will be filled then . The semantics are not obvious, and the code flow does not represent the execution process well.

2. Generator

function* G () {    const A = yield    console.log (' A ', a)  //A AAA    const B = yield of    console.log (' B ', b)  //b bbb    const C = yield    console.log (' C ', c)  //C Ccc}const g = g () g.next ()    //value:100, Done:falseg.next (' AAA ')//value:200, Done:falseg.next (' BBB ')//value:300, Done:falseg.next (' CCC ')//Value:undefin Ed, done:true.

The way of Generator solves some problems of Promise, the process is more intuitive and semantic. The problem with Generator, however, is that the execution of the function depends on the executor, and each time it needs to be executed g.next() in a way.

Third, ES7

1. async/await

Async function TestResult () {Let first    = await doubleafter2seconds ();    Let second = await doubleafter2seconds (a);    Let third = await doubleafter2seconds (+);    Console.log (first + second + third);}

asyncThe function perfectly solves the problem of the above two ways. The process is clear, intuitive and semantically explicit. Manipulating asynchronous processes is like manipulating the synchronization process. At async the same time the function comes with an actuator and does not need to be loaded manually.

Reference Link: 1190000010244279

JS asynchronous Task Processing Mode 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.