Returning a value from inside Fibers(nodejs)

夙愿已清 提交于 2021-02-08 10:35:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!