[streams] listen() (#341)

calling `read()` multiple times gives more control but it's kinda hard to consume when you really don't need to handle backpressure (eg. I just want to `console.log` data as it comes)

```js
var reader = stream.getReader();
reader.listen(logData);
// also need a way to stop listening
reader.unlisten(logData);
```

I know it goes against the promise-based API but it's much simpler to use for simple cases. Right now you would need to do:

```js
function listen(readableStream, cb) {
  const reader = readableStream.getReader();

  return pump();

  function pump() {
    return reader.read().then(({ value, done })=> {
      if (done) {
        return;
      }

      cb(value);
      return pump();
    });
  }
}
```

Maybe the `listen()` method would only be available on a special kind of reader (`getEventReader()`)

PS: this is exactly the same boilerplate as the [`getReader()` example](https://streams.spec.whatwg.org/#rs-get-reader), maybe it's really the time to abstract this logic.

PPS: `listen()` was inspired by [dart:async:Stream](https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.Stream#id_listen)

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/341

Received on Wednesday, 22 April 2015 14:33:57 UTC