Home > Web Front-end > JS Tutorial > body text

How to implement Generator asynchronously in JS

一个新手
Release: 2017-10-11 09:57:46
Original
1782 people have browsed it

1. Symbol data type

The Symbol data type is newly added in ES6.

data Types: array, array array, map, set,

#[Symbol.iterator] attribute object, you can generate an Iterator object by calling it directly
2. Iterator object: It is a pointer object, iterator object

Method: next(): The returned format is {value: , done: }

Use for..of to traverse: Each time you traverse, you get the value.


3. Geneteror

  function*  G(){
             yield    200;
             yield    300;
            return  400;
       }
    var g=G();
Copy after login
1. Generator is not a function. When defining generator, use function* and yield to return result.

2. After calling

Generator

, the code inside it will not be executed immediately, but will be in a suspended state. And generate a generator object. 3. When

encounters

yield, it will execute the expression following yeild and return the value after execution, and then Enter the paused state again, at this time done: false
Generator returns an iterator object, so you can use the next method, for. .of

## 1. When using the next method, the parameters in next will be passed to the value before the executed yield.

2.yield* can reference another Generator in one Generator.

4. Generator implements asynchronous operation

1. A thunk function is required, with only one parameter, the callback function

2. The co library is usually used to package asynchronous operations, which is convenient and concise.

3.Yield must be followed by a thunk function.

4. Generator still relies on callback

## 5. async-await: It is the syntactic sugar of Generator

Compared with Generator:

1. Replace function* with async function

2. Replace yield with await

3. The await is followed by the promise object. It is also OK with other types of data, but it will be executed synchronously instead of asynchronously.

The above is the detailed content of How to implement Generator asynchronously in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!