Skip to content

ES6 Generators work in node but not in the browser #206

Open

Description

@phiresky

for example, the following

Lazy(function*(){yield 1; yield 2;}).forEach(x => console.log(x))

works fine in node:

1
2
> undefined

but not in the JS console on http://danieltao.com/lazy.js/ :

> true

Same for 0.5.0

Activity

  1. dtao commented on Feb 7, 2018

    @dtao
    Owner

    I think this is a result of the way lazy is packaged up, which is frankly a mess. I'll mark this as a bug for now and come back to this soon as I want to clean the whole thing up.

  2. collinsauve commented on Feb 22, 2019

    @collinsauve

    Here's a work-around I'm using:

    function generatorToSequence(generatorFn) {
        function createIterator() {
            const generator = generatorFn();
            let current = null;
            function moveNext() {
                current = generator.next();
                return !current.done;
            }
            return {
                moveNext,
                current: () => current.value
            };
        }
        const seq = new Lazy.Sequence();
        seq.getIterator = () => createIterator();
        return seq;
    }
    

    Then your example:

    generatorToSequence(function*(){yield 1; yield 2;}).forEach(x => console.log(x))
    

    See https://jsfiddle.net/8p7Lmsrn/

  3. nealeu commented on Sep 10, 2020

    @nealeu

    Hi @dtao,

    If you get a look at this, perhaps consider renaming for NPM as having .js in the package name causes issues in webpack. Perhaps going down the route of using an @ prefix (e.g. @lazy.js/core) and splitting things up a bit may be an option to allow subsets to be consumed and good tree-shaking.

    I'm sure there are plenty of us that could pitch in on this sort of effort. I often work with @djcsdy who I'm sure will have some ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions