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

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

Open
phiresky opened this issue Mar 27, 2017 · 3 comments
Open

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

phiresky opened this issue Mar 27, 2017 · 3 comments
Labels

Comments

@phiresky
Copy link

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

@dtao
Copy link
Owner

dtao commented Feb 7, 2018

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.

@collinsauve
Copy link

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/

@nealeu
Copy link

nealeu commented Sep 10, 2020

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
Labels
Projects
None yet
Development

No branches or pull requests

4 participants