Tag: ecmascript 8

如何使用ES8asynchronous/等待与stream?

在https://stackoverflow.com/a/18658613/779159中是如何使用内置的encryption库和stream来计算文件的md5的示例。 var fs = require('fs'); var crypto = require('crypto'); // the file you want to get the hash var fd = fs.createReadStream('/some/file/name.txt'); var hash = crypto.createHash('sha1'); hash.setEncoding('hex'); fd.on('end', function() { hash.end(); console.log(hash.read()); // the desired sha1sum }); // read all file and pipe it (write it) to the hash object fd.pipe(hash); 但是,有可能将此转换为使用ES8asynchronous/等待,而不是像上面看到的使用callback,但仍然保持使用stream的效率?