11
https://stackoverflow.com/a/18658613/779159は、組み込みの暗号ライブラリとストリームを使用してファイルのmd5を計算する方法の例です。ES8 async/streams with a streamsを使用するには?
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);
しかし、代わりに、上記見たコールバックを使用する非同期/のawait ES8を使用してこれを変換することが可能であるが、依然としてストリームの利用効率を維持しながら?
'非同期/ await'が約束の構文レベルでのサポート以外の何ものでもありません:
は今、あなたはその約束を待つことができます。あなただけのストリームをラップする必要があると思います。このコードを約束の中に置くことができれば、あなたは完了です。 –