在generator函数内部,经过yield*
语句,能够将yield
委托给其余任何实现iterable
的对象。javascript
iterable
对象调用generator函数会返回一个实现iterable
的对象(该对象同时也是一个iterator
)。java
经过yield* otherGenerator()
能够将generator内的yield
委托给其余generator生成的iterable
对象。数组
function* foo() {
console.log('*foo() starting');
yield 'foo 1';
yield 'foo 2';
console.log('*foo() finished');
}
function* bar() {
yield 'bar 1';
yield 'bar 2';
yield* foo(); // `yield`-delegation!
yield 'bar 3';
}
var it = bar();
it.next().value;
// "bar 1"
it.next().value;
// "bar 2"
it.next().value;
// *foo() starting
// "foo 1"
it.next().value;
// "foo 2"
it.next().value;
// *foo() finished
// "bar 3"
复制代码
能够看到上面的代码中,在调用第3个next
方法时返回的是foo
里面yield
的"foo 1"
;在调用第5个next
时,并无返回foo
generator隐式return
的undefined
,而是返回了"bar 3"
。ide
若是foo
内有显式的return
语句,那么在进行yield-delegation时是否会返回foo
return
的值吗?函数
下面的代码在foo
中添加了一个return
语句。在bar
内将yield* foo()
表达式的值赋值给tmp
变量并打印。ui
function* foo() {
console.log('*foo() starting');
yield 'foo 1';
yield 'foo 2';
console.log('*foo() finished');
return 'foo 3'
}
function* bar() {
yield 'bar 1';
yield 'bar 2';
var tmp = yield* foo(); // `yield`-delegation!
console.log('在bar内打印', tmp);
yield 'bar 3';
}
var it = bar();
it.next().value;
// "bar 1"
it.next().value;
// "bar 2"
it.next().value;
// *foo() starting
// "foo 1"
it.next().value;
// "foo 2"
it.next().value;
// *foo() finished
// 在bar内打印 foo 3
// "bar 3"
复制代码
在第5次调用next
方法时,能够发现foo
return
的"foo 3"
变成了yield* foo()
表达式的值,并被打印为"在bar内打印 foo 3"
;"bar 3"
成为next
方法的返回值。this
iterable
的对象generator内部的yield*
语句也能将yield
委托给其余非generator生成的iterable
对象。例如 数组就是一个实现了iterable
的对象。url
function* bar() {
console.log("inside `*bar()`:", yield "A");
// `yield`-delegation to a non-generator!
console.log("inside `*bar()`:", yield* ["B", "C", "D"]);
console.log("inside `*bar()`:", yield "E");
return "F";
}
var it = bar();
console.log("outside:", it.next().value);
// outside: A
console.log("outside:", it.next(1).value);
// inside `*bar()`: 1
// outside: B
console.log("outside:", it.next(2).value);
// outside: C
console.log("outside:", it.next(3).value);
// outside: D
console.log("outside:", it.next(4).value);
// inside `*bar()`: undefined
// outside: E
console.log("outside:", it.next(5).value);
// inside `*bar()`: 5
// outside: F
复制代码
也能够委托给本身手写的iterable
对象。因为javascript不是强类型语言,若是对象上含有Symbol.iterator
方法,那么就能够将该对象当作一个iterable
对象;若是对象上含有next
方法,那就能够将该对象当作一个iterator
对象。下面的myIterable
对象即实现了Symbol.iterator
方法也实现了next
方法,因此它便是一个iterable
又是一个iterator
。spa
var myIterable = {
[Symbol.iterator]: function () {
return this;
},
num: 98,
next: function () {
var self = this;
if (self.num < 101) {
return { value: String.fromCharCode(self.num++), done: false };
} else {
return { value: String.fromCharCode(101), done: true };
}
}
}
function* bar() {
console.log("inside `*bar()`:", yield "A");
// `yield`-delegation to a non-generator!
console.log("inside `*bar()`:", yield* myIterable);
console.log("inside `*bar()`:", yield "E");
return "F";
}
var it = bar();
console.log("outside:", it.next().value);
// outside: A
console.log("outside:", it.next(1).value);
// inside `*bar()`: 1
// outside: b
console.log("outside:", it.next(2).value);
// outside: c
console.log("outside:", it.next(3).value);
// outside: d
console.log("outside:", it.next(4).value);
// inside `*bar()`: e
// outside: E
console.log("outside:", it.next(5).value);
// inside `*bar()`: 5
// outside: F
复制代码
被委托的iterator
内部执行过程发生异常,若是异常被捕获,那么捕获处理完异常后还按原来的逻辑运行;若是异常未被捕获,那么异常会被抛出,异常会被抛到yield*
语句那。下面是《YOU DON'T KNOW JS》内的例子。code
function* foo() {
try {
yield "B";
}
catch (err) {
console.log("error caught inside `*foo()`:", err);
}
yield "C";
throw "D";
}
function* bar() {
yield "A";
try {
yield* foo();
}
catch (err) {
console.log("error caught inside `*bar()`:", err);
}
yield "E";
yield* baz();
// note: can't get here!
yield "G";
}
function* baz() {
throw "F";
}
var it = bar();
console.log("outside:", it.next().value);
// outside: A
console.log("outside:", it.next(1).value);
// outside: B
console.log("outside:", it.throw(2).value);
// error caught inside `*foo()`: 2
// outside: C
console.log("outside:", it.next(3).value);
// error caught inside `*bar()`: D
// outside: E
try {
console.log("outside:", it.next(4).value);
}
catch (err) {
console.log("error caught outside:", err);
}
// error caught outside: F
复制代码
下面是《YOU DON'T KNOW JS》里的例子。
function* foo(val) {
if (val > 1) {
// generator recursion
val = yield* foo(val - 1);
}
return yield request("http://some.url/?v=" + val);
}
function* bar() {
var r1 = yield* foo(3);
console.log(r1);
}
run(bar);
复制代码