How to use ES8 async/await with streams?

后端 未结 3 645
我寻月下人不归
我寻月下人不归 2020-11-28 04:56

In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams.

var fs = re         


        
相关标签:
3条回答
  • 2020-11-28 05:07

    If you are using node version >= v10.0.0 then you can use stream.pipeline and util.promisify.

    const fs = require('fs');
    const crypto = require('crypto');
    const util = require('util');
    const stream = require('stream');
    
    const pipeline = util.promisify(stream.pipeline);
    
    const hash = crypto.createHash('sha1');
    hash.setEncoding('hex');
    
    async function run() {
      await pipeline(
        fs.createReadStream('/some/file/name.txt'),
        hash
      );
      console.log('Pipeline succeeded');
    }
    
    run().catch(console.error);
    
    0 讨论(0)
  • 2020-11-28 05:18

    Something like this works:

    for (var res of fetchResponses){ //node-fetch package responses
        const dest = fs.createWriteStream(filePath,{flags:'a'});
        totalBytes += Number(res.headers.get('content-length'));
        await new Promise((resolve, reject) => {
            res.body.pipe(dest);
            res.body.on("error", (err) => {
                reject(err);
            });
            dest.on("finish", function() {
                resolve();
            });
        });         
    }
    
    0 讨论(0)
  • 2020-11-28 05:26

    async/await only works with promises, not with streams. There are ideas to make an extra stream-like data type that would get its own syntax, but those are highly experimental if at all and I won't go into details.

    Anyway, your callback is only waiting for the end of the stream, which is a perfect fit for a promise. You'd just have to wrap the stream:

    var fd = fs.createReadStream('/some/file/name.txt');
    var hash = crypto.createHash('sha1');
    hash.setEncoding('hex');
    // read all file and pipe it (write it) to the hash object
    fd.pipe(hash);
    
    var end = new Promise(function(resolve, reject) {
        hash.on('end', () => resolve(hash.read()));
        fd.on('error', reject); // or something like that. might need to close `hash`
    });
    

    Now you can await that promise:

    (async function() {
        let sha1sum = await end;
        console.log(sha1sum);
    }());
    
    0 讨论(0)
提交回复
热议问题