Re: [whatwg/streams] Add examples of using identity transforms to manipulate streams (#847)

ricea commented on this pull request.



> +</code></pre>
+
+The error handling here is subtle because cancelling the concatenated stream has to cancel all the intput
+streams. However, the success case is simple enough. We just pipe each stream in the <code>readables</code> array one at
+a time to the transform stream's <a>writable side</a>, and then close it when we are done. The <a>readable side</a> is
+then a concatenation of all the chunks from all of of the streams. We return it from the function. Backpressure is
+applied as usual.
+
+It's sometimes natural to treat a promise for a readable as if it were a readable. A simple adaptor function is all
+that's needed:
+
+<pre><code class="lang-javascript">
+function promiseToReadable(promiseForReadable) {
+  const ts = new TransformStream();
+  promiseForReadable.then(readable => readable.pipeTo(ts.writable))
+      .catch(reason => ts.writable.abort(reason))

It can fail if `readable` doesn't implement pipeTo(), or if pipeTo() is implemented but throws, or readable is locked... probably some more cases too.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/847#discussion_r147908327

Received on Tuesday, 31 October 2017 07:04:20 UTC