-2

I've been reading a lot about callbacks, but still don't get it...

I'm executing an async function which adds to an array a string with info about the available disk space.

All this code is in a script, and I'd like to be able to use that array in others. So far I tried to return it and pass it as parameter... but it executes before finishing the task.

var diskspace = require('diskspace');
var fs = require('fs');
var aux;

function getDiskSpace(json){
    diskspace.check('C',function(err, total, free, status){ 
        aux=new Object();
        aux.name="diskspace";
        aux.value=(((total-free)/(1024^3)).toFixed(2)+"MB used, "+(free*100/total).toFixed(2)+"% free");
        json.push(aux);
    }); 
    aux= new Object();
    aux.name="date";
    aux.value=(new Date().toLocaleString());
    json.push(aux);
    aux= new Object();
    aux.name="arduinos";
    aux.value=JSON.parse(fs.readFileSync("./data/dirs.json","utf8"));
    json.push(aux); 
}

module.exports=getDiskSpace;

Once in the main program, I send it like JSON:

    var array=new Array();
    var getDiskSpace=require('./getIndexInfo.js');
    getDiskSpace(array);
    res.writeHead(200,{"Content-Type": "application/json"});
    res.end(JSON.stringify(array));

Can you tell me the proper way to do this? I know this have been discussed a lot, but I've been reading even about promises also, and the more I read the more confused I am, sorry.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • http://blog.slaks.net/2015-01-04/async-method-patterns/ – SLaks Jun 01 '15 at 16:28
  • Wow, the title's of the dupe questions are so close, it makes me think the OP didn't bother to look. – Adam Jenkins Jun 01 '15 at 16:28
  • I've checked all those questions, but still I couldn't get the answer. @Adam your comment makes me think you didn't bother to read further than the title. Anyway, the first answer is the solution, thanks to all ;) – perez_chuck Jun 01 '15 at 17:19

1 Answers1

0

For any asyn function you need to have a callback function which gets executed once the operation is complete. You can modify your code like this and try.

var diskspace = require('diskspace');
var fs = require('fs');
var aux;

function getDiskSpace(cb){
    diskspace.check('C',function(err, total, free, status){ 
      var arr = [];
      var aux=new Object();
      aux.name="diskspace";
      aux.value=(((total-free)/(1024^3)).toFixed(2)+"MB used, "+(free*100/total).toFixed(2)+"% free");
      arr.push(aux);

      aux= new Object();
      aux.name="date";
      aux.value=(new Date().toLocaleString());
      arr.push(aux);

      aux= new Object();
      aux.name="arduinos";
      aux.value=JSON.parse(fs.readFileSync("./data/dirs.json","utf8"));
      arr.push(aux); 

      //Pass the array to the callback
      //In case of any error pass the error info in the first param
      cb(null, arr);
    });
}

module.exports=getDiskSpace;

Usage

var getDiskSpace=require('./getIndexInfo.js');
getDiskSpace(function (err, arr) {
  ///If err is not null then send error response
  res.writeHead(200,{"Content-Type": "application/json"});
  res.end(JSON.stringify(arr));
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124