profile
viewpoint

Ask questionsSummit Topic: Node.js Streams promise support

<!-- Thank you! You are submitting a topic for the next Collaborator's Summit, Montreal 2019!

Please include as much detail as you are able to at this moment. Don't worry, it doesn't have to be complete.

Please feel free to link to any other issue, PR, or resource that could be relevant. -->

Topic of the session

We have added async iterators to stream a quite successfully! It's time to think of what's missing.

Type of the session

  • [x] Collaborate
  • [ ] Workshop
  • [ ] Talk

Follow-up / Set-up sessions (if any)

List the sessions that are related.

Level

  • [] Beginner
  • [] Intermediate
  • [] Advanced

Pre-requisite knowledge

List pre-requisite knowledge that it would be required for participants to have.

Describe the session

Session facilitator(s) and Github handle(s)

<!-- Here's a handy guide for the person or persons who will facilitate this session. -->

Additional context (optional)

openjs-foundation/summit

Answer questions apapirovski

I feel like something like this works reasonably well, whether you care about errors or not:

Writable.prototype.write[promisify.custom] = () =>
  function promisifiedWrite(chunk, encoding) {
    let syncErr;
    const needDrain = !this.write(chunk, encoding, (err) => {
      syncErr = err
    });

    const stream = this;
    return {
      then() {
        if (syncErr) {
          throw syncErr;
        }

        if (needDrain) {
          return new Promise((resolve, reject) => {
            stream.once('error', reject);
            stream.once('drain', () => {
              stream.removeEventListener(reject);
              resolve();
            });
          });
        }
      }
    };
  };

Wouldn't leave it as a promisified function though, ideally it's first-class.

useful!

Related questions

Summit Topic: Promises in Node.js Core hot 1
Summit Topic: Streams (Node.js) hot 1
Community Corner: How to contribute to open source using Git and GitHub hot 1
Summit Topic: Streams (Node.js) hot 1
Github User Rank List