Search code examples
ecmascript-6yieldcreate-react-app

create-react-app doesn't support `yield`?


I created a project using create-react-app but when I write some code with the keyword yield, the page shows building error claiming 'Unexpected token' at yield.

enter image description here

Is there a way to enable yield in create-react-app projects?


Solution

  • Did you forget to mark the function as a Generator by using the * symbol?

    function fn() {
        yield "some value";
    }
    

    This code results in a syntax error, since the yield keyword is not allowed in normal functions.

    function *generatorFn() {
        yield "some value";
    }
    

    This code does not result in a syntax error, since the function is a generator.

    For an example see this snippet