ES6生成器:.next()的输入值

Shi*_*shi 2 javascript ecmascript-6

我正在关注这个很棒的博客,并且有一个简单的生成器示例,它为该.next()方法发送一个值:

var answers = [
  `It is certain`, `It is decidedly so`, `Without a doubt`,
  `Yes definitely`, `You may rely on it`, `As I see it, yes`,
  `Most likely`, `Outlook good`, `Yes`, `Signs point to yes`,
  `Reply hazy try again`, `Ask again later`, `Better not tell you now`,
  `Cannot predict now`, `Concentrate and ask again`,
  `Don't count on it`, `My reply is no`, `My sources say no`,
  `Outlook not so good`, `Very doubtful`
]
function answer () {
  return answers[Math.floor(Math.random() * answers.length)]
}

// The most relevant part
function* chat () {
  while (true) {
    let question = yield '[Genie] ' + answer()
    console.log(question)
  }
}
Run Code Online (Sandbox Code Playgroud)

我真的无法理解这个输入将如何产生这个输出:

var g = chat()
g.next()
console.log(g.next('[Me] Will ES6 die a painful death?').value)
// <- '[Me] Will ES6 die a painful death?'
// <- '[Genie] My sources say no'
Run Code Online (Sandbox Code Playgroud)

第一个输出来自哪里 - 结果的[Me]部分?

Pat*_*ans 5

之后的表达式yield将会退出(返回).传递给的值next()进入,yield表达式求值为此值.请注意,虽然在第一个.next()调用中传入的任何值都会被丢弃但不会被使用.

  1. g.next() 叫做

    let question = yield '[Genie] ' + answer()    
                         ^-------------------^
          execution is paused here, this expression is evaluated
          and the result will go out(returned)
               {value:"[Genie] ...",done:false}
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后g.next('[Me] Will ES6 die a painful death?')被叫

    g.next('[Me] Will ES6 die a painful death?').value
           ^----------------------------------^
     string goes in, execution continues, yield expression 
          will be evaluated as the passed in string
    
     let question = yield '[Genie] ' + answer(); //becomes -v
     let question = '[Me] Will ES6 die a painful death?';
    
    Run Code Online (Sandbox Code Playgroud)
  3. question等于[Me]字符串

  4. question 通过登录到控制台 console.log(question)
  5. 执行继续,直到遇到下一个产量

    let question = yield '[Genie] ' + answer()    
                         ^-------------------^
          execution is paused here, this expression is evaluated
          and the result will go out(returned)
               {value:"[Genie] ...",done:false}
    
    Run Code Online (Sandbox Code Playgroud)
  6. 现在g.next('[Me] Will ES6 die a painful death?')评估为

    {value:"[Genie] ...",done:false}
    
    Run Code Online (Sandbox Code Playgroud)

    最后一行评估如下:

    console.log( g.next('[Me] Will ES6 die a painful death?').value );
    //becomes 
    console.log( ({value:"[Genie] ...",done:false}).value );
    //which becomes
    console.log("[Genie] ...");
    
    Run Code Online (Sandbox Code Playgroud)