Why is ES6 “yield” a reserved word when called in this context?

前端 未结 5 2122
别跟我提以往
别跟我提以往 2020-12-09 09:34

I am using node 4.1.1. When I run this code

\"use strict\";

function *generator() {
  let numbers = [1,2,3,4,5];
  numbers.map(n => yield (n + 1));
}

f         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 10:07

    That's because the arrow function is not a generator. If I expand your arrow function, it would look something like:

    function *generator() {      // <-- this is your generator function
      let numbers = [1,2,3,4,5];
      numbers.map(function(n){   // <-- this one isn't a generator
        yield (n + 1)            // <-- there's your yield
      }.bind(this));
    }
    

提交回复
热议问题