Stop running processes after a Promise is rejected

前端 未结 2 776
刺人心
刺人心 2021-02-07 02:34

I\'m using the following code which working OK, but the problem is that when I get an error, I want it to stops all the other promises. For example if chi.getCommand(val1,

2条回答
  •  一个人的身影
    2021-02-07 03:04

    Well, in your actual code (the one from UPDATE1) you are running getCommand concurrently to getStatus, not in sequence. You're calling (starting) both of them before the child process fails, and when it does there is nothing that would stop getStatus.

    Just chain them together like in your first snippet, where a rejection in getCommand will cause getStatus to not run at all. You can use

    childP.getCommand('spawn', cmd)
    .timeout(5000)
    .then(function(cmdresult) {
        return app.getStatus(51000, 10, 1);
    }).catch(function (err) {
        console.log("An error occured: " + err);
    });
    

提交回复
热议问题