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

throat alike #262

Open
jimmywarting opened this issue Feb 20, 2016 · 1 comment
Open

throat alike #262

jimmywarting opened this issue Feb 20, 2016 · 1 comment

Comments

@jimmywarting
Copy link

Think it would be useful if you could implement some like throat so you could specify how many parallel yields is possible

consider this example

co(function* (){
    const book = fetch("books/mobidick")
    const chapters = book.chapters
    let pages = []

    // use 5 paralle http request at the same time
    yield co(5, function* {
        for(chapter of chapters){
            let page = yield fetch("books/mobidick/chapters/" + chapter.index)
            pages[chapter.index] = page
        }
    })

    return pages
})
@epoberezkin
Copy link

That's not possible, really. The idea is that the value that yield returns is resolved and the code continues to execute synchronously.

For parallel requests you need to yield an array (or object) with promises, so your example can be this:

co(function* (){
    const book = fetch("books/mobidick")
    const chapters = book.chapters
    let pages = []

    chapters.forEach((chapter) => {
        pages[chapter.index] = fetch("books/mobidick/chapters/" + chapter.index);
    });
    return yield pages;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants