Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to await writable streams #19068

Closed
targos opened this issue Mar 1, 2018 · 3 comments
Closed

Allow to await writable streams #19068

targos opened this issue Mar 1, 2018 · 3 comments
Labels
promises Issues and PRs related to ECMAScript promises. stream Issues and PRs related to the stream subsystem.

Comments

@targos
Copy link
Member

targos commented Mar 1, 2018

With async iterators, we can use synchronous-looking code to consume readable streams.
There is no equivalent for writable streams in Node core.

Example use case:

const fs = require('fs');
const zlib = require('zlib');

(async () => {
  const read = fs.createReadStream('data.txt.gz');
  const gunzip = zlib.createGunzip();
  const write = fs.createWriteStream('data.txt');

  await read.pipe(gunzip).pipe(write);
})();

This would resolve when write ends or reject if an error occurs.

Prior art:

/cc @nodejs/streams @mcollina

@targos targos added stream Issues and PRs related to the stream subsystem. promises Issues and PRs related to ECMAScript promises. labels Mar 1, 2018
@mcollina
Copy link
Member

mcollina commented Mar 1, 2018

This is currently not possible because of how error handling work in streams.

However, wrapping #13506 (stream.pump) in a promise should be simple to do. I think we should work on landing that first, and then adding a promisified version of that too (or util.promisify()).

@mafintosh
Copy link
Member

Will be possible with the new pipeline api (based on pump) in #19828 and util.promisify, assuming it lands at it's current form

const stream = require('stream')

(async () => {
  const pipeline = util.promisify(stream.pipeline)
  const read = fs.createReadStream('data.txt.gz');
  const gunzip = zlib.createGunzip();
  const write = fs.createWriteStream('data.txt');

  await pipeline(read, gunzip, write);
})();

@targos
Copy link
Member Author

targos commented Apr 17, 2018

Fixed in #19828

@targos targos closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
promises Issues and PRs related to ECMAScript promises. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants