在承诺链中调用两种方法

Call for two method in promise chain

我有以下有效的承诺

troces.run(value, "../logs/env.txt")
    .then(function (data) {
        console.log(data);
        return updadeUser(val, arg, args[1])
        // Now here I need to add new method updateAddress(host,port,addr)

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

现在我需要在第一个 .then 中添加额外的方法调用 更新用户和 updateAddress 将一起工作

我的问题是

  1. assume that updateUser will need to start 10 ms after the update address How is it recommended to do so?

  2. in aspects of error handling if one of the process failed (send error message ) I Need to exit (process.exit(1);)

使用.all:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return Promise.all([updadeUser(val, arg, args[1]),
                        updateAddress(host,port,addr)]);
}); // no need to add catches bluebird will log errors automatically

如果你真的需要10毫秒的延迟,你可以这样做:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return Promise.all([updadeUser(val, arg, args[1]),
                        Promise.delay(10).then(x => updateAddress(host,port,addr))]);
}); // no need to add catches bluebird will log errors automatically

尽管我 怀疑 你真的只是想让 updateUser updateAddress 之前发生,这很容易解决方法:

troces.run(value, "../logs/env.txt")
.then(data => {
    console.log(data);
    return updadeUser(val, arg, args[1]).then(_ => updateAddress(host,port,addr));
}); // no need to add catches bluebird will log errors automatically

如果你需要在 promise 错误时退出,你可以这样做:

process.on("unhandledRejection", () => process.exit(1)); 

虽然我强烈建议您创建有意义的错误消息,但 non-zero 进程退出代码很难调试。