如何在节点JS中的另一个函数中返回回调函数值

时间:2020-10-30 12:55:49

标签: javascript node.js typescript callback diskspace

我正在尝试编写一个监视磁盘和内存使用情况的程序,并在磁盘或内存使用率超过80%时发出警告。我想在另一个条件上返回磁盘使用率。这是我的程序。预先感谢。

const os = require('os');
const disk = require('diskusage');


const CheckMemory = () =>{
    const total_memory = os.totalmem(); 

    const free_memory = os.freemem();

    const used_memory = total_memory - free_memory;

    const usedmemper = used_memory * 100 / total_memory; 
    return usedmemper.toFixed(2)+" %";
}

const CheckDisk = () =>{
    disk.check('/', function(err, info) {
        const Total_space = info.total / Math.pow(1024, 3);
        const free_space = info.free;
        console.log("this is free space "+ free_space )
        return "This is total disk "+ Total_space.toFixed(2)+ " Gb";
        
    });
}


const Warning = (mem) =>{
    let i = parseFloat(mem)
    console.log(i)
    if (i >=  80) {
        console.log("You are currently using 80% of your available memory");
    }
} 


const Usedram = CheckMemory();
Warning(Usedram);
CheckDisk();

0 个答案:

没有答案