Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: yield is a reserved word #1624

Closed
msageryd opened this issue May 18, 2017 · 7 comments
Closed

SyntaxError: yield is a reserved word #1624

msageryd opened this issue May 18, 2017 · 7 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@msageryd
Copy link

I get "SyntaxError: yield is a reserved word" from Prettier.

The function is a generator function used in a ReduxSaga:

function* saveUnsavedJournalItemsToServer() {
  const journalItemsToSave = yield select(selectCompletedUnsavedJournalItems);
  journalItemsToSave.map(journalItem => {
    yield call(saveJournalItemToServer, journalItem);
  });
}

Here is the complete output.

journalSagas.js: SyntaxError: yield is a reserved word (177:4)
  175 |
  176 |   journalItemsToSave.map(journalItem => {
> 177 |     yield call(saveJournalItemToServer, journalItem);
      |     ^
  178 |   });
  179 | }
  180 |

Is this a Prettier bug, or is my code flawed?

@k15a
Copy link
Collaborator

k15a commented May 18, 2017

You call yield in a normal function but it is only allowed in a generator. I might be wrong but I think the correct code is:

function* saveUnsavedJournalItemsToServer() {
  const journalItemsToSave = yield select(selectCompletedUnsavedJournalItems);
  yield* journalItemsToSave.map(function* (journalItem) {
    yield call(saveJournalItemToServer, journalItem);
  });
}

@msageryd
Copy link
Author

Ah, thanks.
That makes sense. Didn't see that I wrapped it in a normal function.

So, Prettier did a good job catching the problem. Maybe a better error message would be good, if possible.

@k15a
Copy link
Collaborator

k15a commented May 18, 2017

That's the error message from either babel or flow because prettier uses them to parse your code. :)

If you want to learn more about generators take a look at http://exploringjs.com/es6/ch_generators.html

@msageryd
Copy link
Author

Thanks again.
You guessed it: I'm a generator newbie =)

I'll read up some more on the subject. Also, the "yield call" will probably make funny stuff happen. I have now changed to "yield fork".

@msageryd
Copy link
Author

This is maybe a rare occation where a good old for-loop comes in handy (instead of map or each). No messing around with nested generators needed.

@vjeux
Copy link
Contributor

vjeux commented May 18, 2017

The error message is indeed super confusing. If you could open an issue on babylon and flow projects to get it improved that would be awesome!

@msageryd
Copy link
Author

msageryd commented May 18, 2017

babel/babylon#530

I don't know if Flow would give this error as well. I don't use Flow anymore, so I presume it was Babel who gave the error.

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 7, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

3 participants