1

I'm getting this error in a react-redux-saga project:

Syntax error: yield is a reserved word (66:16)

 function* broken(action) { 
  props.forEach(prop => {                                                    
    const res = yield put(blah)   
    // do stuff yada yada in here                                                                           
  })
}
ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61
  • Re the related question: it doesn't mention the error message I was getting... also it fails to succinctly illustrate both problem and solution. – ErichBSchulz Mar 24 '18 at 13:41
  • *"it doesn't mention the error message I was getting"* Cool, now this question does. That doesn't make it not a duplicate. *"also it fails to succinctly illustrate both problem and solution"* Er...yes, it does. – T.J. Crowder Mar 24 '18 at 13:42
  • 1
    @T.J.Crowder - sorry bud - your definition of succinct must be different to mine ;-) – ErichBSchulz Mar 24 '18 at 13:46
  • Sorry, I meant it has an illustration of the problem and solution. :-) Succinctness would be good, but doesn't make it not a dupetarget for this question. If you think the existing answers there are not clear enough, post an answer there. – T.J. Crowder Mar 24 '18 at 13:48
  • Huh? I haven't modified the linked question. I'm sorry if you don't like SO's definition of a duplicate question (if the answers there answer this question, it's a duplicate), but that quarrel isn't with me. – T.J. Crowder Mar 24 '18 at 14:00
  • 1
    This should be part of meta discussion, but I don't necessarily agree that the answer dictates whether a question is a duplicate. *Q1: Where are we going? A: The store. Q2. Where do you buy bread? A: The store* — the same answer to two different questions. For me, this question is much more concise and easier to follow, while the other question is much more descriptive. Ordinarily, I might call it a duplicate, but believe there is value for longevity in this question. So shouldn't some discretion be used? – vol7ron Mar 24 '18 at 19:16

1 Answers1

1

Turns out the internal function needs to be a generator too - but that then causes, erm, problems. So best to use a standard loop with no callbacks. Something like:

function* working(action) { 
  for (const prop of props) {
    const res = yield put(blah) 
    // do stuff yada yada in here                                                                           
  }
}
ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61
  • 2
    And that does what you want it to do? – H.B. Mar 24 '18 at 13:35
  • well that exact code doesn't - I refactored and simplified it to be enough to illustrate the broken the and working format... i'll clarify – ErichBSchulz Mar 24 '18 at 13:37
  • Isn't the return value of `yield x` always `undefined`? That inner line makes no sense to me. – H.B. Mar 24 '18 at 13:42
  • @T.J.Crowder: Indeed, noticed that as well. Unless you have side-effects of course. – H.B. Mar 24 '18 at 13:44
  • 1
    @H.B.: Even then. If you have side-effects, use `forEach` (or `for-of`). :-) – T.J. Crowder Mar 24 '18 at 13:44
  • But using `forEach` with a generator function makes no sense either. `map` could if one were using the resulting array of iterators, but... – T.J. Crowder Mar 24 '18 at 13:50
  • 1
    @H.B. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield – ErichBSchulz Mar 24 '18 at 13:50
  • Good to know, but since your code does not do that it may have been best to not have that there, not that it matters much. – H.B. Mar 24 '18 at 13:54
  • @T.J.Crowder goo point on `map`... I've changed the code to `forEach` - I need a bunch of `put`s generated - how exactly do you propose to generate them given I have an array of tasks to call? sorry I wasn't posting this to get a full code review - but just because this was one of the few instances where SO didn't rapidly point out the error of my ways - even despite a review of existing questions – ErichBSchulz Mar 24 '18 at 13:54
  • Do you really need a generator though? I never really use them. If you want to yield a list as individual elements you can use `yield * props.map(x => put(x));`, by the way. – H.B. Mar 24 '18 at 13:57
  • @ErichBSchulz: Your issue with the dupetarget is that the answers aren't good enough. And yet the above code doesn't make sense. I'm sorry if you have an issue with people pointing that out. If you really want to add to SO's information on this point, I strongly recommend writing a good, clear, answer with sensical code -- and posting it to the dupetarget. Good day. – T.J. Crowder Mar 24 '18 at 14:02
  • you're right @T.J.Crowder my initial "fixed" code, and the first patch weren't actually working - this is unrelated to the fact the "dupe" makes no mention of stated error... and indeed the brokeness of my first attempts is best explained by an unrelated question. I still contend that this is not a dupe - even if my initial answers were broken. – ErichBSchulz Mar 24 '18 at 14:34
  • @H.B. - thanks for the suggestion but I actually want multiple yields - not a single yield of an array – ErichBSchulz Mar 24 '18 at 14:35