async-stream icon indicating copy to clipboard operation
async-stream copied to clipboard

async

Open juliangruber opened this issue 11 years ago • 3 comments
trafficstars

@Raynos can you please explain what kind of async streaming is missing?

function(){
  return function*(){
    return yield fs.readFile(nextLog)
  }
}

juliangruber avatar Feb 04 '14 18:02 juliangruber

@juliangruber you are using yield both for yielding a value from the stream and for doing something asynchronous

For example you need to open a file descriptor asynchronously. That requires a callback or a yield. You can't use a yield because you don't want to yield a file descriptor object, you just want to open it.

You can't use a callback because you can't yield in a callback

Raynos avatar Feb 04 '14 19:02 Raynos

Interesting, I re-read the spec. Your implementing a stream as a stateful generator function that will return a value from the stream each time it's called.

That is actually possible to do, hmm.

Raynos avatar Feb 04 '14 19:02 Raynos

There are probably cases where this is awkward too but so far if all is pulling that's cool and if a source is pusing you can use https://github.com/segmentio/co-queue to buffer:

function wrap(emitter, event){
  var queue = new Queue;
  emitter.on(event, queue.push);
  return function*(end){
    if (!end) return yield queue.next;
  };
}

var read = wrap(emitter, 'data');
while (true) console.log(yield read());

juliangruber avatar Feb 04 '14 21:02 juliangruber