Nodejs Promise.all()始终解析

时间:2015-06-10 11:35:53

标签: javascript node.js promise es6-promise

我是承诺的新手。我试图ping一些机器来检查它们是否处于活动状态。我正在使用本机NodeJS承诺。我的ping功能:

function ping(addr) {
    return new Promise(function(resolve, reject) {
        var args = ['-n', '1', '-w', '5000'];
        args.push(addr);
        var ls = cp.spawn('ping.exe', args);

        ls.on('error', function (e) {
            reject(Error('There was an error while executing the ping'));

        });

        ls.on('exit', function (code) {
            if(code === 0) {
                resolve({host: addr});
            } 
            else {
                reject(Error(addr + " is  down!"));
            }

        });
    });

}

现在,我有一个从JSON读取的数组中的机器的详细信息:

gulp.task('pingNodes', ['readConfigJSON'], function () {

    var batches = ConfigJSON.NodeDetails.Batch1.concat(ConfigJSON.NodeDetails.Batch2);
    var pingPromises = batches.map(function (host) {
        return ping(host.Name)
            .then(function (res) {
                console.log(res.host + " is  up!");
            }).catch(function (err) {
                console.log(err);
            });
    });
    return Promise.all(pingPromises).then(function(){console.log("All nodes are up!")});
});

即使某个节点出现故障,它现在也不会拒绝:

[16:58:46] Starting 'init'...
Starting Deployment
[16:58:46] Finished 'init' after 135 µs
[16:58:46] Starting 'readConfigJSON'...
[16:58:46] Finished 'readConfigJSON' after 204 µs
[16:58:46] Starting 'pingNodes'...
machine1 is  up!
machine2 is  up!
machine3 is  up!
[Error: machine4 is  down!]
All nodes are up!
[16:58:49] Finished 'pingNodes' after 2.54 s

1 个答案:

答案 0 :(得分:5)

<强>解决方案

要解决此问题,请在catch处理程序中再次抛出错误,如此

}).catch(function (err) {
    console.log(err);
    throw err;
});

或删除catch处理程序。基本上,你应该让拒绝流向链条,以便Promise.all在机器停机时获得拒绝承诺。

基本理解

  1. 所有thencatch处理程序都会创建一个新的Promise对象并返回它们,以便我们可以将它们链接起来。

  2. 当拒绝承诺时,拒绝处理程序将处理它,但如果拒绝处理程序不拒绝承诺,则链中的后续处理程序不会将承诺视为拒绝。 / p>

  3. 在您的情况下,当机器关闭时,您拒绝它并拒绝处理,

    }).catch(function (err) {
        console.log(err);
    });
    

    但是,catch处理程序返回一个未被拒绝的承诺。因此,Promise.all实际上获得了一个未被拒绝的Promise对象。这就是为什么一旦发现其中一台机器出现故障就不会停止。

    您可以使用以下程序确认此理解

    var a = Promise.resolve(1)
        .then(function (e) {
            throw e;
        })
        .catch(function (e) {
            // This is what you are doing if the machine is down
            console.log(e);
    
            // No rejection here, so `then` will be called.
            // Uncomment the throw to see 
            // throw e;
        });
    
    a.then(function (e) {
        console.log("Inside then")
    });
    
    a.catch(function (e) {
        console.log("Inside catch")
    });
    
相关问题