Discuss / JavaScript / 为何generator 最后一个不返回?

为何generator 最后一个不返回?

Topic source

_Again_

#1 Created at ... [Delete] [Delete and Lock User]
function* foo(x) {
    yield x + 1;
    yield x + 2;
    return x + 3;
}

for (var x of foo(100)) {
    console.log(x); 
    // 理论应该依次输出101, 102, 103 
    // 实际输出101, 102 疑惑 
}

细雨小巷

#2 Created at ... [Delete] [Delete and Lock User]

for...of...循环只输出yield;也就是done是false的语句;return done就是true了,所以不输出这个。


  • 1

Reply