如何在JS中处理嵌套的promise返回?

时间:2017-07-08 14:51:18

标签: javascript node.js es6-promise

我的代码如下,基本上,我正在尝试打印系统信息并创建返回对象。所以当我调用函数console.log(this.refresh())时不打印JS对象,而它正在函数体中打印。我很迷惑。另外,我是JS Promise Concepts的新手,我想我在下面编写了愚蠢的代码,请提供一些见解以优化下面的代码。

 // npm install systeminformation --save

 const si = require('systeminformation');
 var beautify = require("json-beautify");
 var flag = true;

 var info = {};
 exports.refresh = function () {

     si.system()
         .then(data => {
             delete data['serial'];
             delete data['uuid'];
             info.system = data;
             si.cpu()
                 .then(data => {
                     info.cpu = data;
                     si.mem()
                         .then(data => {
                             info.mem = formatUserFriendlyMemory(data, flag);
                             si.osInfo()
                                 .then(data => {
                                     delete data['hostname'];
                                     info.osInfo = data;
                                     si.fsSize()
                                         .then(data => {
                                             info.fileSystem = formatUserFriendlyFileSystem(data[0], flag);
                                             //console.log(beautify(info, null, 2, 80))
                                             var i = beautify(info, null, 2, 80);
                                            // console.log(i)
                                             return i;
                                         })
                                         .catch(error => console.error(error));
                                 })
                                 .catch(error => console.error(error));
                         })
                         .catch(error => console.error(error));
                 })
                 .catch(error => console.error(error));
         })
         .catch(error => console.error(error));
 }




 function formatUserFriendlyMemory(data, flag) {
     if (!flag) return data;
     data.total = (data.total) / 1024 / 1024 / 1024 + " GB";
     data.free = (data.free) / 1024 / 1024 + " MB";
     data.used = (data.used) / 1024 / 1024 + " MB";
     data.active = (data.active) / 1024 / 1024 + " MB";
     data.available = (data.available) / 1024 / 1024 + " MB";
     data.buffcache = (data.buffcache) / 1024 / 1024 + " MB";
     return data;
 }

 function formatUserFriendlyFileSystem(data, flag) {
     if (!flag) return data;
     data.free = data.size - data.used;
     data.size = (data.size) / 1024 / 1024 / 1024 + " GB";
     data.free = (data.free) / 1024 / 1024 / 1024 + " GB";
     data.used = (data.used) / 1024 / 1024 / 1024 + " MB";
     return data;
 }


var res = this.refresh();
console.log(res);
  

输出 - 未定义

Inside Function I can See the object
     Sample OutPut - 

     {
       "system": {
         "manufacturer": "Apple Inc.",
         "model": "MacBookPro12,1",
         "version": "1.0"
       },
       "cpu": {
         "manufacturer": "Intel®",
         "brand": "Core™ i5-5257U",
         "vendor": "GenuineIntel",
         "family": "6",
         "model": "61",
         "stepping": "4",
         "revision": "",
         "speed": "2.70",
         "speedmin": "2.70",
         "speedmax": "2.70",
         "cores": 4,
         "cache": { "l1d": 32768, "l1i": 32768, "l2": 262144, "l3": 3145728 }
       },
       "mem": {
         "total": "8 GB",
         "free": "62.94921875 MB",
         "used": "8129.05078125 MB",
         "active": "2868.88671875 MB",
         "available": "5323.11328125 MB",
         "buffcache": "5260.1640625 MB",
         "swaptotal": 0,
         "swapused": 0,
         "swapfree": 0
       },
       "osInfo": {
         "platform": "Darwin",
         "distro": "Mac OS X",
         "release": "10.12.5",
         "codename": "",
         "kernel": "16.6.0",
         "arch": "x64",
         "logofile": "apple"
       },
       "fileSystem": {
         "fs": "/dev/disk1",
         "type": "HFS",
         "size": "111.859375 GB",
         "used": "100.70209121704102 MB",
         "use": 90.03,
         "mount": "/",
         "free": "11.157283782958984 GB"
       }
     }
  

更新了返回内容的代码 -

exports.refresh = function () {

     return si.system()
         .then(data => {
             delete data['serial'];
             delete data['uuid'];
             info.system = data;
             si.cpu()
                 .then(data => {
                     info.cpu = data;
                     si.mem()
                         .then(data => {
                             info.mem = formatUserFriendlyMemory(data, flag);
                             si.osInfo()
                                 .then(data => {
                                     delete data['hostname'];
                                     info.osInfo = data;
                                     si.fsSize()
                                         .then(data => {
                                             info.fileSystem = formatUserFriendlyFileSystem(data[0], flag);
                                             //console.log(beautify(info, null, 2, 80))
                                             var i = beautify(info, null, 2, 80);
                                             // console.log(i)
                                             return i;
                                         })
                                         .catch(error => console.error(error));
                                     return;
                                 })
                                 .catch(error => console.error(error));
                             return;
                         })
                         .catch(error => console.error(error));
                     return;
                 })
                 .catch(error => console.error(error));
             return;
         })
         .catch(error => console.error(error));
 }





 var res = this.refresh();
 console.log(res);

解决这个问题的方法就是链接。代码如下。

 const si = require('systeminformation');
 var beautify = require("json-beautify");
 var flag = true;


 var refresh = function () {
     var info = {};

     return getSystem(info).then(getSystem).then(getOSInfo).then(getMem).then(getCPU).then(getFileSZ);


     function getSystem() {
         return si.system().then(data => {
             delete data['serial'];
             delete data['uuid'];
             info.system = data;
             return info;
         });
     }

     function getCPU() {
         return si.cpu().then(data => {
             info.cpu = data;
             return info;
         });

     }

     function getMem() {
         return si.mem().then(data => {
             info.memory = formatUserFriendlyMemory(data, flag);
             return info;
         });

     }

     function getOSInfo() {
         return si.osInfo().then(data => {
             delete data['hostname'];
             info.osInfo = data;
             return info;

         });
     }

     function getFileSZ() {
         return si.fsSize().then(data => {
             info.fileSystem = formatUserFriendlyFileSystem(data[0], flag);
             return info;
         });
     }


 }

 refresh().then(info => {
     console.log(JSON.stringify(info, null, ' '))
 }).
 catch(err => {
     console.log('Error While Refreshing the System Status')
 })



 function formatUserFriendlyMemory(data, flag) {
     if (!flag) return data;
     data.total = (data.total) / 1024 / 1024 / 1024 + " GB";
     data.free = (data.free) / 1024 / 1024 + " MB";
     data.used = (data.used) / 1024 / 1024 + " MB";
     data.active = (data.active) / 1024 / 1024 + " MB";
     data.available = (data.available) / 1024 / 1024 + " MB";
     data.buffcache = (data.buffcache) / 1024 / 1024 + " MB";
     return data;
 }

 function formatUserFriendlyFileSystem(data, flag) {
     if (!flag) return data;
     data.free = data.size - data.used;
     data.size = (data.size) / 1024 / 1024 / 1024 + " GB";
     data.free = (data.free) / 1024 / 1024 / 1024 + " GB";
     data.used = (data.used) / 1024 / 1024 / 1024 + " MB";
     return data;
 }

0 个答案:

没有答案