所以我在尝试从这个模块的构造函数返回一个值时遇到了麻烦。我真的不知道如何将值返回给构造函数。
我希望代码是不言自明的,我希望构造函数返回散列变量。
var Fiber = require('fibers');
var Future = require('fibers/future'), wait = Future.wait;
var fs = require('fs')
var crypto = require('crypto')
exports.cryptFile = function(fileName, callback){
var crypt = crypto.createHash('md5');
var stream = fs.ReadStream(fileName, {
bufferSize: 1024 * 1024
});
stream.on('data', function(data) {
crypt.update(data)
});
stream.on('error', function(err) {
callback(err);
});
stream.on('end', function(){
var digest = crypt.digest('hex')
callback(null, digest);
});
}
var cryptFileFuture = Future.wrap(this.cryptFile);
module.exports = function(filename){
var future = new Future;
Fiber(function(){
hash = cryptFileFuture(filename).wait();
console.log(hash);
future.return(hash);
});
return future;
// return hash;
}