JavaScript ES6 Generators: input value for .next() python generator input value

I'm following this awesome blog and there is a simple example of generators with sending a value for the .next() method:

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)
  }
}

I really can't understand how this input will produce this output:

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'

Where does the first output comes from - the [Me] part of the result?

Answer:1

The expression after yield will go out(returned). The value passed to next() goes in, and the yield expression evaluates to this value. Note though any value passed in on the first .next() call is thrown away and not used.

  1. g.next() is called

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

    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?';
    
  3. question equals the [Me] string

  4. question gets logged to console by console.log(question)
  5. Execution continues until the next yield is encountered

    let question = yield '[Genie] ' + answer()    
                         ^-------------------^
          execution is paused here, this expression is evaluated
          and the result will go out(returned)
               {value:"[Genie] ...",done:false}
    
  6. Now g.next('[Me] Will ES6 die a painful death?') evaluates to

    {value:"[Genie] ...",done:false}
    

    Making the last line evaluating as such:

    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] ...");
    
Answer:2

When I'm trying to do object-oriented pure JS with private methods, I frequently do the following (function(exports) { var Foo = function(num) { this.num = num }; var addTwo = function() {...

When I'm trying to do object-oriented pure JS with private methods, I frequently do the following (function(exports) { var Foo = function(num) { this.num = num }; var addTwo = function() {...

  1. private object functions
  2. javascript private functions in object

I have some text on a page which I get using a variable: <p class="category-list-replace">ksadj| lksd| sakdj</p> <p class="category-list-replace">sdawfe| nekesa</p> <p ...

I have some text on a page which I get using a variable: <p class="category-list-replace">ksadj| lksd| sakdj</p> <p class="category-list-replace">sdawfe| nekesa</p> <p ...

  1. replace characters html
  2. replace html characters javascript
  3. replace html characters c#
  4. replace html characters java
  5. replace html characters php
  6. replace html characters in sql
  7. replace html characters swift
  8. replace special characters html
  9. vba replace html characters
  10. js replace html characters
  11. python replace html characters
  12. mysql replace html characters
  13. oracle replace html characters
  14. jquery replace html characters
  15. html replace characters in a string
  16. replace special characters with html entities
  17. java string replace html characters
  18. php replace special characters html entities
  19. vb.net replace html characters
  20. javascript replace special characters html entities

I have a Table with JQuery's selectable feature.My Table was selectable each td which are in the same column. Below is my full html <html> <head> <link href="/css/jquery-ui....

I have a Table with JQuery's selectable feature.My Table was selectable each td which are in the same column. Below is my full html <html> <head> <link href="/css/jquery-ui....

When using a for...of loop to iterate over an HTMLCollection returned from DOMParser only the odd elements are returned. I don't think this happens with NodeLists or HTMLCollections not created with ...

When using a for...of loop to iterate over an HTMLCollection returned from DOMParser only the odd elements are returned. I don't think this happens with NodeLists or HTMLCollections not created with ...