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

Support for yielding streams along with promises by generator functions in Q.spawn #777

Open
ajtejankar opened this issue Apr 28, 2016 · 2 comments

Comments

@ajtejankar
Copy link

ajtejankar commented Apr 28, 2016

I've been using Q.spawn along with generators for a while now and I love it. The spawn method expects the generator function to return a promise on each iteration. My question is does it make sense to support streams also?. Streams are common in nodejs and will be coming to browser environments shortly. Also, since promises are basically a special case of streams it makes sense to support them.

@benjamingr
Copy link
Collaborator

Promises are not a special case of streams any more than numbers are a special case of arrays of numbers.

That said, what would yielding streams even look like?

If they're generic enough there are helpers that let you convert a stream to a promise for its completion event.

@ajtejankar
Copy link
Author

I may have explained myself badly when I said that promises are a special case of streams. What I meant was that a stream is a sequence of values in the future while a promise is a single value in the future. This analogy also links a number and an array of numbers together albeit with a different link. The spatial link. Please have a look the table in this paragraph for broader framework under which my statements fall.

Q.spawn(function* () {
  yield fs.createReadStream('x.txt')
    .pipe(fs.createWriteStream('y.txt'));
});

This is what yielding a stream would look like. In the above example execution is halted until the stream ends. The spawn function would attach a event handler on event end or finish and call the next method (I don't know whether it should call it with a concatenated value of stream chunks or nothing).

I've come up with above example based on a recent use case. I don't have enough experience with streams, iterators or generators to suggest the precise behavior but if the aim of using spawn is to write async code that looks synchronous then streams must be handled too. If there is no support for yielding a stream then I have to yield a promise that resolves when the stream ends.

Q.spawn(function* () {
  let fromStream = fs.createReadStream('x.txt');
  let toStream = fs.createWriteStream('y.txt');

  yield new Promise(resolve => {
    toStream.on('end', () => resolve());
    fromStream.pipe(toStream);
  });
});

If I end up using a lot of streams this way then I might consider some helper that converts the end of a stream into a promise.

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