-1

Title says it all.

Output is:

starting
lol
undefined
0 undefined

Expected:

starting // yield giving us back a string
lol  // yield giving back result from bar()
'apples' // since the 'yield bar()' is 'replaced' by 'apples' via next('apples')
'0 apples' // the last yield should tell us what fruit is, but its undefined

Code:

const bar = () => 'lol'

function* foo1() {
  yield 'starting'
  let a = 0
  let fruit = yield bar()
  console.log(fruit) // <-- y no apples? I could even see 'lol' being here too.
  yield `${a} ${fruit}`

}

let g = foo1()

console.log(g.next().value)
console.log(g.next('apples').value)
console.log(g.next().value)
CRice
  • 29,968
  • 4
  • 57
  • 70
jason
  • 4,721
  • 8
  • 37
  • 45
  • 5
    Generators are initially paused. The first `next` starts them. – Ry- Nov 10 '21 at 19:04
  • 2
    I see that two people understood your comment, but it doesn't seem obvious to me. – Robert Harvey Nov 10 '21 at 19:06
  • 1
    Why the downvote people? There's running code, expected output.... – Ruan Mendes Nov 10 '21 at 19:10
  • @jason No it's not, downvotes are intended to point out that a question is poorly asked. This question fulfills all the requirements. No need to bash SO because you got downvoted a few times :D However, yes, if you're going to downvote, a comment is often helpful so the OP or the person replying can improve their question/answerq – Ruan Mendes Nov 10 '21 at 19:15
  • @jason Do you feel like the single answer to the top duplicate doesn’t directly answer your question (and more than that, with a question asking about the exact problem)? – Ry- Nov 10 '21 at 19:16
  • It does not. I do start off the generator with a next(). – jason Nov 10 '21 at 19:17
  • Im trying to decipher your meaning though, the 'initially paused' part. – jason Nov 10 '21 at 19:18
  • @Ry I also don't see it, I think an answer explaining what's going on here would be helpful, even to seasoned developers – Ruan Mendes Nov 10 '21 at 19:18
  • @jason: And then you pause it again with a `yield 'starting'`. If you already knew about the behaviour it’s just an error of counting. The third call to `next` corresponds to the second `yield`. – Ry- Nov 10 '21 at 19:18
  • Possibly, but I think maybe I dont understand passing in stuff via next('stuff'). – jason Nov 10 '21 at 19:19
  • 1
    Try passing in a separate value with every `next` call and it might be a bit clearer. `g.next('a').value`, `g.next('b').value`, `g.next('c').value` – Ry- Nov 10 '21 at 19:19
  • Ahhhh i see. tytyty – jason Nov 10 '21 at 19:21
  • 1, `.next()` unpauses the generator, runs `yield 'starting', pauses. 2. `.next('apples')` unpauses the generator, runs `yield bar()`, pauses. 3. `.next()` unpauses the generator *runs the assignment*... – VLAZ Nov 10 '21 at 19:23
  • 1
    Yep. VLAZ for the win. – jason Nov 10 '21 at 19:23
  • 1
    For anyone finding this, move the 'apples' to the next next() statement, and it all works as intended. The part I was missing (that Ry and VLAZ educated me on) was that the yield pauses BEFORE it assigns to fruit. Meditate on that and it all comes together. Thanks yall. :) – jason Nov 10 '21 at 19:27

1 Answers1

-1

According to Mozilla Docs function*

// the first call of next executes from the start of the function until the first yield statement

You must 'init' with a first next() call before getting yield statement

const bar = () => 'lol'

function* foo1() {  
  yield 'starting'
  let a = 0
  let fruit = yield bar()
  console.log(fruit)  // <-- y no apples? I could even see 'lol' being here too.
  yield `${a} ${fruit}`
  
}

let g = foo1()
g.next();
console.log( g.next().value)
console.log( g.next('apples').value )
bZezzz
  • 972
  • 9
  • 22