cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

Promises vs iterators? #365

Closed dthorpe closed 10 years ago

dthorpe commented 10 years ago

What is the recommended way to operate on promises returned via an iterator/generator? I have an iterator with a next() that returns { value: Promise, done: boolean }.

when.iterate() and when.unfold() don't seem to be compatible with iterators, since they seem to require that the seed be the data, the factory, and the condition.

Have I missed something obvious? Any pointers greatly appreciated.

briancavalier commented 10 years ago

Hey @dthorpe. Right, when.iterate and when.unfold were originally created before JS iterators were implemented anywhere. We have another project, most.js, which handles asynchronous sequences. It's built on when.js promises, and embraces ES6 iterators/iterables.

It can handle Promises for iterations, and iterations whose value is a Promise (like your example above). So, for example, you can write:

// Create an async stream from a iterable whose iterator values are promises
// Extract the value from each promise, filter them, take the first 10, map them,
// and then reduce them down into a single result, which will also be a Promise.
most.from(iterableWithPromisesForValues).flatMap(most.fromPromise)
    .filter(...)
    .take(10)
    .map(...)
    .reduce(....);

It's a very natural compliment to promises. We're actively working on it, and we just released 0.7. Obviously, we need to spend time on documentation, but the project is moving pretty quickly right now, so I expect more docs to appear quickly. For now, have a look at the examples dir.

Please take a look and let us know what you think!

dthorpe commented 10 years ago

Thanks!

Yes, docs for most.js would be helpful. ;>

briancavalier commented 10 years ago

Totally agree :)

The JSDoc is quite comprehensive and complete (eg scroll through the main file), so for now, that is a good place to start looking. It's no substitute for more readable docs, but hopefully useful in the meantime.

I just opened cujojs/most#29, so I'll close this issue.