How to use ES8 async/await with streams?

后端 未结 3 668
我寻月下人不归
我寻月下人不归 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: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();
            });
        });         
    }
    

提交回复
热议问题