function* test(){
console.log("start");
yield;
console.log("finish");
}
var fun1 = test(); //须将函数声明为变量使用此方法
fun1.next();
fun1.next();
yield*
yield和yield类似,只不过他是可以自动解构,用法如下
function go(){
yield* [1, 2, 3];
yield* 'Hi';
yield* Array.from(arguments);
}
var g = go(4, 5);
console.log(g.next()); // {value: 1, done: false}
console.log(g.next()); // {value: 2, done: false}
console.log(g.next()); // {value: 3, done: false}
console.log(g.next()); // {value: "H", done: false}
console.log(g.next()); // {value: "i", done: false}
console.log(g.next()); // {value: 4, done: false}
console.log(g.next()); // {value: 5, done: false}
console.log(g.next()); // {value: undefined, done: true}
网友评论