当前位置:网站首页>Generator Foundation
Generator Foundation
2022-07-06 07:33:00 【Romantic bearded boy】
One 、 Basic grammar
//* It means that there will be yield
function* Function name () {
// Function content
yield;
}
Two 、 call Generator function
Generator Functions are the same as ordinary functions , It's all through Function name () To make the call :
Function name ();
however , Generator The function is called after , Does not execute internal code . But it will return an iterator object .
const it = Function name ();
next step , Calling iterator's next() Method to enter the function and execute the internal code of the function .
it.next();
3、 ... and 、yield
yield It can be used to block the execution of code in functions .
function* myGenerator() {
console.log('1、myGenerator Yes ');
yield;
console.log('2、myGenerator Yes ');
}
const it = myGenerator();
it.next();
it.next();
yield It can be followed by an expression :
function* myGenerator() {
console.log('1、myGenerator Yes ');
yield 'hello';
console.log('2、myGenerator Yes ');
yield 'world';
console.log('3、myGenerator Yes ');
}
const it = myGenerator();
console.log(it.next());
console.log(it.next());
next() You can also pass parameters , This parameter will be used as the current yield The return value of :
function* myGenerator() {
console.log('1、myGenerator Yes ');
const res = yield 'hello';
console.log('res', res);
}
const it = myGenerator();
console.log(it.next());
console.log(it.next("world"));
Four 、 Deal with asynchronous
边栏推荐
猜你喜欢
随机推荐
JMeter performance test steps practical tutorial
2022年Instagram运营小技巧简单讲解
C - Inheritance - hidden method
杰理之BLE【篇】
Detailed explanation | detailed explanation of internal mechanism of industrial robot
Brief explanation of instagram operation tips in 2022
[computer skills]
C intercept string
Bit operation XOR
Three treasures of leeks and Chinese men's football team
[MySQL learning notes 29] trigger
C # connect to SQLite database to read content
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
多线程和并发编程(二)
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】
Do you really think binary search is easy
Basics of reptile - Scratch reptile
NiO programming introduction
[MySQL learning notes 32] mvcc
Scala语言学习-08-抽象类