co icon indicating copy to clipboard operation
co copied to clipboard

Be able to yield an iterable for parallel execution

Open dashed opened this issue 9 years ago • 5 comments

My usecase with collections from https://github.com/facebook/immutable-js:

co.wrap(function*(list) {

    // list is Immutable.Map

    const results = yield list.map(function(id) {
        return new Promise(function(resolve, reject) {
            // ...
        });
    }

    // ...
});

dashed avatar Sep 10 '15 17:09 dashed

You can use Promise.all: yield Promise.all(iterable). That's also closer to what you'd do with async functions, which seems like the ultimate endgame.

cesarandreu avatar Sep 13 '15 05:09 cesarandreu

Thanks. I hadn't thought of Promise.all. I've been reducing iterables to an array.

dashed avatar Sep 14 '15 07:09 dashed

:+1: for Promise.all - this issue can probably be closed. Since es7 async functions exist now, I wouldn't recommend adding many features that diverge from that line.

tejasmanohar avatar Oct 11 '15 19:10 tejasmanohar

Are you saying that Promise.all is better than yield <array of promises> (as map returns array)?

And also in yield <array of promises>, promises don't run in parallel?

Thanks

harshitgupta avatar Oct 14 '16 18:10 harshitgupta

It seems like co's yield <array of promises> diverges in behavior from await <array of promises>

// co:
var result = yield [Promise.resolve(1)];
result // => [1]

// async/await:
var result = await [Promise.resolve(1)];
result // => [Promise.resolve(1)]

wrapping either with Promise.all would have the same effect

kentor avatar May 12 '17 06:05 kentor