简体   繁体   English

如何从 forEach 循环中实现扁平化的 promise 数组?

[英]How can I achieve a flattened array of promises from a forEach loop?

I am attempting to utilize a chain of promises (via Q & the node child-process-promise library) to pull drive letters, free space, and total size utilizing the Windows WMIC command like so:我正在尝试利用一系列承诺(通过 Q 和节点子进程承诺库)利用 Windows WMIC 命令提取驱动器号、可用空间和总大小,如下所示:

var stdout = '';
var exec = require('child-process-promise').exec;

exec('wmic logicaldisk where drivetype=3 get caption', { capture: ['stdout', 'stderr'] })
    .then(function(result) {
        var data = result.stdout.split('\r\n');
        data = data.splice(1, data.length - 3);
        data = data.map(Function.prototype.call, String.prototype.trim);
        data.forEach(function(drive) {
            // This is where I am getting hung up
        });
    })
    .then(function(result) {
        console.log(result);
    })
    .fail(function(err) {
        console.error(err);
    });

As you can in the code above, I am returning an array of drive letters from the initial execution of WMIC, though after this I will need to run WMIC several more times depending on the count of drives returned by the first query.正如您在上面的代码中所做的那样,我从 WMIC 的初始执行中返回了一个驱动器号数组,但在此之后,我将需要根据第一个查询返回的驱动器数量再运行几次 WMIC。 For instance, if my data array looks like so:例如,如果我的数据数组如下所示:

['C:','D:']

I'll need to run:我需要运行:

wmic logicaldisk where caption="C:" get freespace
wmic logicaldisk where caption="C:" get size
wmic logicaldisk where caption="D:" get freespace
wmic logicaldisk where caption="D:" get freespace

Though I would like to do this from a flat promise chain to aggregate the results, I cannot seem to wrap my head around how to do this.虽然我想从一个扁平的承诺链来聚合结果,但我似乎无法理解如何做到这一点。 I've seen several other questions where utilizing a "reduce" is suggested, but can't seem to figure how to apply this to my current situation.我已经看到其他几个问题,其中建议使用“减少”,但似乎无法弄清楚如何将其应用于我目前的情况。

Create a collection of Promises by iterating over the data and storing the result of each call to exec in an array.通过迭代数据并将每次调用exec的结果存储在一个数组中来创建一个 Promises 集合。 Then wait for the resolution of all the Promises by passing the array to Q#all .然后通过将数组传递给Q#all来等待所有 Promise 的解析。

Using nested forEach :使用嵌套forEach

var promises = [];

data.forEach(function (drive) {
    var commands = [
        'wmic logicaldisk where caption="' + drive + '" get freespace',
        'wmic logicaldisk where caption="' + drive + '" get size'
    ];

    commands.forEach(function (cmd) {
        promises.push(exec(cmd, {capture: ['stdout', 'stderr']}));
    });
});

return Q.all(promises);

Or using reduce and map :或者使用reducemap

var promises = data.reduce(function (result, drive) {
    var commands = [
        'wmic logicaldisk where caption="' + drive + '" get freespace',
        'wmic logicaldisk where caption="' + drive + '" get size'
    ].forEach(function (cmd) {
        result.push(exec(cmd, {capture: ['stdout', 'stderr']}));
    });

    return result;
}, []);

return Q.all(promises);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM