JS-ES6 Learning Notes Asynchronous application of-generator function

Source: Internet
Author: User
Tags readfile

1, before the birth of ES6, asynchronous programming methods, there are probably the following four kinds.

    • callback function
    • Event Monitoring
    • Publish/Subscribe
    • Promise Object

The Generator function brings JavaScript asynchronous programming into a whole new phase.

2, the so-called "asynchronous", simply said that a task is not continuous completion, it can be understood that the task is divided into two segments, first execute the first paragraph, and then to perform other tasks, and so ready to go back to the second paragraph.

3, JavaScript language to asynchronous programming implementation, is the callback function. The so-called callback function is to write the second paragraph of the task separately in a function, and then call this function directly when the task is re-executed. The English name of the callback function callback literal is called "recall".

4, the callback function of the asynchronous way easy to form multiple nesting, multiple asynchronous operations to form a strong coupling, as long as an operation needs to be modified, its upper callback function and the underlying callback function, may have to follow the modification. This situation is called "Callback function Hell" (Callback Hell).

5, Promise object is to solve this problem and put forward. It is not a new syntax function, but a new way of writing, allowing the nesting of callback functions to be changed into chained calls. Using Promise, read multiple files continuously, the following is the wording.

var readFile = require (' fs-readfile-promise '); ReadFile (FileA). Then (function  (data) {  Console.log (data.tostring ());}). Then (function  () {  return  readFile (Fileb);}). Then (function  (data) {  Console.log (data.tostring ());}). Catch (function  (err) {  console.log (err);});

As you can see, the Promise is just the improvement of the callback function, after the use of the then method, the asynchronous task of the two-segment execution to see more clearly, in addition, there is no novelty.

6, the traditional programming language, early asynchronous programming solutions (in fact, a multi-tasking solution). One of these is called "coroutine", meaning that multiple threads work together to complete an asynchronous task.

7, Generator function is the implementation of the ES6, the biggest feature is the ability to hand over the execution of functions (that is, suspend execution).

The entire Generator function is a encapsulated asynchronous task, or a container for an asynchronous task. The place where the asynchronous operation needs to be paused is indicated by a yield statement.

8. The Generator function can pause execution and resume execution, which is the root cause of its ability to encapsulate asynchronous tasks. In addition, it has two features that allow it to act as a complete solution for asynchronous programming: data exchange and error handling mechanisms outside the body of the function.

9. The value next property of the return value of the method is the output data of the Generator function outward; The next method can also accept parameters and enter data into the Generator function body.

10, the compiler's "Bogwang call" implementation, often put the parameters into a temporary function, and then pass this temporary function into the function body. This temporary function is called the Thunk function.

function F (m) {  return m * 2+ 5); // equivalent to var function () {  return x + 5;}; function F (thunk) {  return thunk () * 2;}

This is the definition of the Thunk function, which is an implementation strategy for the "Bogwang call", which replaces an expression.

11, JavaScript language is a value call, its Thunk function meaning is different. In the JavaScript language, the Thunk function replaces not an expression, but a multi-parameter function, replacing it with a single-argument function that accepts only the callback function as a parameter.

// Normal version of ReadFile (multi-parameter version) Fs.readfile (FileName, callback); // thunk version of ReadFile (single-parameter version) var function (fileName) {  returnfunction  (callback) {    return  Fs.readfile (FileName, callback);  } ; var readfilethunk = Thunk (fileName); Readfilethunk (callback);

12, the use of thunk function, to be continued

JS-ES6 Learning Notes Asynchronous application of-generator function

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.