question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promisify Await WriteStream ReadStream Streams

See original GitHub issue

Hi @jprichardson @RyanZim I want to await the end of a stream. Are there any plans to add that? Would you be interested in a PR? If yes, should the api be to return the same old e.g. WriteStream with an added .promise() that can be awaited?

Edit - maybe the promises should be the names of the events? E.g. await fse.createWriteStream(path).end()?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

14reactions
ubershmekelcommented, Jun 21, 2017

Hi @RyanZim,

The nodejs s3 api only gave me a stream. I wanted to download a file to disk so I can process it in ffmpeg. So I needed to know when the file was done downloading.

Here’s what I ended up with:


function streamPromise(stream) {
    return new Promise((resolve, reject) => {
        stream.on('end', () => {
            resolve('end');
        });
        stream.on('finish', () => {
            resolve('finish');
        });
        stream.on('error', (error) => {
            reject(error);
        });
    });
}

async function s3Download(srcBucket, srcKey, outputPath) {
    var objReq = s3.getObject({
        Bucket: srcBucket,
        Key: srcKey
    });
    let out = fse.createWriteStream(outputPath);
    objReq.createReadStream().pipe(out);
    return streamPromise(out);
}

I didn’t do the due diligence to figure out the difference between finish and end but it worked so I moved on.

4reactions
timm-gscommented, Nov 24, 2017

Just to add another data point: I was also looking for this functionality.

I have a stream.Readable from a http response and want to write it to a file. I would think in a Node.js environment it is common to work with streams, so having native support in this library would be useful. (I would be worried that not everybody will write their own streamPromise function like above, but rather read the whole stream into a Buffer to then be able to use the standard fs-extra functions…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use ES8 async/await with streams? - Stack Overflow
async / await only works with promises, not with streams. There are ideas to make an extra stream-like data type that would get...
Read more >
Understanding Streams in Node.js | NodeDev
createWriteStream () lets us write data to a file using streams. Readable: streams from which data can be read. For example: fs.
Read more >
Easier Node.js streams via async iteration - 2ality
Asynchronous iteration is a protocol for retrieving the contents of a data container asynchronously (meaning the current “task” may be paused ...
Read more >
Wrap your streams with promises for fun and profit
Now streamToFile immediately returns a promise object that we can pass around, await in some other async function, or chain .then and .catch ......
Read more >
Stream | Node.js v19.3.0 Documentation
Consuming readable streams with async iterators; Creating readable streams with ... createWriteStream('file.txt'); // All the data from readable goes into ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found