问题
So i'm having trouble trying to return a value from the constructor of this module. I literally have no idea how to return a value back to the constructor.
I hope the code is self-explanatory, i want the constructor to return the hash variable.
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;
}
回答1:
you forgot the .run()
on on the Fiber
来源:https://stackoverflow.com/questions/19708961/returning-a-value-from-inside-fibersnodejs