Can I yield from an inner function?

后端 未结 2 1692
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 03:46

With ES6 generators, I see code like this:

var trivialGenerator = function *(array) {
    var i,item;
    for(var i=0; i < array.length; i++){
        item =          


        
2条回答
  •  执念已碎
    2021-01-31 04:29

    No, you can't use yield inside of the inner function. But in your case you don't need it. You can always use for-of loop instead of forEach method. It will look much prettier, and you can use continue, break, yield inside it:

    var trivialGenerator = function *(array) {
        for (var item of array) {
            // some item manipulation
            yield item;
        }
    }
    

    You can use for-of if you have some manipulations with item inside it. Otherwise you absolutely don't need to create this generator, as array has iterator interface natively.

提交回复
热议问题