与ES6相似,
function* outer() {
function* inner() {
var a = 100
yield a
}
var gen = inner()
var result = gen.next()
yield result.value
}
function caller() {
var gen = outer()
var result = gen.next()
}
caller()
我可以在没有外部桥接功能的情况下直接在调用方中处理内部生成器的结果吗? 如果不是,是否有更好的设计来解决发电机自动运行和发电机递归问题? 谢谢
另一个问题? python怎么样?
def outer():
def inner():
a = 100
yield a
b = 200
yield b
gen = inner()
result = next(gen)
yield result
# it works not like js version
# yield inner()
def caller():
gen = outer()
result = next(gen)
print("caller result : ", result)
caller()
答案 0 :(得分:2)
在生成器内部使用yield*
时,您可以从另一个 生成器生成所有项目:
function* outer() {
function* inner() {
var a = 100
yield a
}
yield* inner();
}
function caller() {
var gen = outer()
var result = gen.next()
console.log(result);
}
caller()