Re: [streams] Promises based transform() (#185)

Consider the example at https://streams.spec.whatwg.org/#example-rs-pull, in particular:

```js
    pull(enqueue, close, error) {
      const buffer = new ArrayBuffer(CHUNK_SIZE);

      fs.read(fd, buffer, 0, CHUNK_SIZE, position).then(bytesRead => {
        if (bytesRead === 0) {
          return fs.close(fd).then(close);
        } else {
          position += bytesRead;
          enqueue(buffer);
        }
      })
      .catch(error);
    },
```

with promises, this would become

```js
    pull(enqueue, close) {
      const buffer = new ArrayBuffer(CHUNK_SIZE);

      return fs.read(fd, buffer, 0, CHUNK_SIZE, position).then(bytesRead => {
        if (bytesRead === 0) {
          return fs.close(fd).then(close);
        } else {
          position += bytesRead;
          enqueue(buffer);
        }
      });
    },
```

That is, it allows you to return a promise for error propagation. That's kind of nice. Not revolutionary. And I think it introduces a one-turn delay?

However, consider the case of a simple duplicater transform:

```js
transform(chunk, enqueue, done, close, error) {
  enqueue(chunk);
  enqueue(chunk);
  done();
}
```

could become

```js
transform(chunk, enqueue, close) {
  enqueue(chunk);
  enqueue(chunk);
}
```

which is a lot nicer. (But this time you definitely get a one-turn delay before we can call `transform` again.) Async transforms too:

```js
transform(chunk, enqueue, done, close, error) {
  getAsyncData(chunk).then(d => { enqueue(d); done(); }, error);
}
```

becomes

```js
transform(chunk, enqueue, close) {
  return getAsyncData(chunk).then(enqueue);
}
```

One thing that has me confused is that the current transform implementation doesn't pass `close` and `error` params like I do above. Perhaps because we expect the creator to just call `transform.writable.close()` and `transform.writable.abort(er)`? That seems lame. Maybe it was just an oversight.

---

I am actually somewhat convinced now that transform is more like writable than readable, contradicting my position from the OP. So it seems pretty clear that we want to be able to return promises from `transform`.

However, I'm not as sure about whether we want to use promises for readable-stream `pull`. I am leaning toward yes, since I think as long as we still use `enqueue` for enqueuing data, there is no extra one-turn delay. I need to analyze the algorithms a bit more to be sure.

This is still in my sprint for this week; hopefully I can figure out the modification to `ReadableStream` at least this weekend since we want to nail down `ReadableStream` ASAP.

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

Received on Saturday, 24 January 2015 00:20:55 UTC