Meteor.async and futures question - SOLVED

Many thanks for reading:

I am trying (and learning) meteor to enhance the user experience of a small hobbyist (open source)
Python / PHP application which uploads files to a folder and creates links to allow download.

From the meteor side of things I upload files in a mongo collection (base64 - 16 MB max).
After I upload the file , I save it to the filesystem in order to compute the md5 sum of it .
The problem is the mb5 sum is created after the function returns and therefore I have an undefined value
returned to client.
In client side I have:
saveFile: to upload and save file to mongo / filesystem
hashCreate: to calculate server side the hash and return hash to client ():

Template.fileUpload.events({
  "submit"(event,template){
    event.preventDefault();
 //   console.log(template.dataUrl.get());
    Meteor.call("saveFile",{file_uploaded:template.dataUrl.get(),filename:template.filename.get(), file_timestamp:template.lastModified.get()} , 
	function(err, result) {
if(!err) {
         		 
		 lastId=result;
		 console.log(lastId);
		 
		 Meteor.call("hashCreate",{file_uploaded:template.dataUrl.get(),filename:template.filename.get()}, 
		        function(err, result) {
                                         if(!err) {
		                                    var  hash=result;
		                                      console.log(hash);
	                                             //	 Meteor.call("updateMongo", {lastId , hash});
                                                    }
                                         else {
	                                             console.log (error);
	                                           }
		                               } ); 

          } 
	 else 
		  {console.log (error);
	      }
	}) ;
	

	
  } 
});

In server side I have tried several tricks make the following piece of code synchronous to no avail:

My original working version is (server side) :


Meteor.methods({
    

   'saveFile': function(buffer){
   //insert into mongo
    lastId = MyFiles.insert({hash: 0 , file_uploaded:buffer['file_uploaded'],filename:buffer['filename'], file_timestamp:buffer['file_timestamp']})  ;		
  	//write temporarily to filesystem
	let fileContents= buffer['file_uploaded'];
	var path= process.cwd();
	
	fs.writeFileSync(path+'\\' +buffer['filename'], fileContents, 'base64', function(err) {
    if(err) {
        return console.log(err);
    }
    
	}); 
	return lastId;
		},
		 
	'hashCreate':function(buffer) {
        var fileHash;
	var path= process.cwd();
	var fd = fs.createReadStream(path+'\\' +buffer['filename']);
	var hash = crypto.createHash('md5');
        hash.setEncoding('hex');
	    
	var fd = fs.createReadStream(path+'\\' +buffer['filename']);
	var hash = crypto.createHash('md5');
       hash.setEncoding('hex');

        fd.on('end', function() {
               hash.update(buffer['filename'],'utf8');
	       hash.end();
	       fileHash= hash.read(); 
	       console.log(fileHash);
		 });


 fd.pipe(hash);
	
console.log(fileHash);
return fileHash;
},

Problem is that the pipe and .on complete after the function returns and therefore the filehash is not returned
to client since since it is not available when the hashCreate function finishes …
I have tried a FUTURE version (in doubt if it well written from the many tutorials I have read) which again does not return the value after finishing
(although it produces the hash result)

function createHash (buffer)

{
 var fileHash;
var path= process.cwd();
var fd = fs.createReadStream(path+'\\' +buffer['filename']);
var hash = crypto.createHash('md5');
 hash.setEncoding('hex');
	    
var fd = fs.createReadStream(path+'\\' +buffer['filename']);
var hash = crypto.createHash('md5');
 hash.setEncoding('hex');

    fd.on('end', function() {
      hash.update(buffer['filename'],'utf8');
	  hash.end();
	   fileHash= hash.read(); 
	 //  MyFiles.update(lastId ,{ $set: { hash:fileHash },
			 console.log(fileHash);
		return fileHash;

		});
 fd.pipe(hash);

}

and called from client with Meteor.call

'hashCreate':function(buffer) {
Future = Npm.require('fibers/future');
var myFuture = new Future(); 
createHash(buffer , function (err, results ) {
 myFuture.return (results);  
  }); 
     const hash =  myFuture.wait;
    console.log(hash);
    return hash; 
}

My last try is Meteor.async (again I am not very sure about it…) but no luck as well:

'hashCreate':function(buffer) {
  
const encodeSync = Meteor.wrapAsync(createHash);
 const hash = encodeSync(buffer);	
console.log(hash);
	return hash;} etc...

Any help or suggestion greatly appreciated…


EDIT

I have some progress by correcting the function createHash (buffer) definition
since it is not correct:
I have changed it to

createHash=function  (buffer,callback)
{
...
}

and I am calling it with

const encodeSync = Meteor.wrapAsync(createHash);
   const hash = encodeSync(buffer);	

I can not understand exactly how and where I will use a callback in this case.

----------SOLVED --------

The solution was the following

createHash=function  (buffer,callback)

{
	
 var fileHash;
	var path= process.cwd();
	var fd = fs.createReadStream(path+'\\' +buffer['filename']);
	var hash = crypto.createHash('md5');
    hash.setEncoding('hex');

    fd.on('end', function() {
      hash.update(buffer['filename'],'utf8');
	  hash.end();
	   fileHash= hash.read(); 
	
		
    callback(null , fileHash);
		});


 fd.pipe(hash);


	

}

and the call was

'hashCreate':function(buffer) {
  

   const encodeSync = Meteor.wrapAsync(createHash);
   const hash = encodeSync(buffer);	
	
	return hash;

	},