简体   繁体   English

在Promise链中调用两种方法

[英]Call for two method in promise chain

I've the following promise which work well 我有以下承诺,效果很好

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);
    });

Now I need to add additional method call inside the the first .then that the update user and the updateAddress will work together 现在我需要添加的第一个内部附加方法调用.then将更新用户和updateAddress将共同努力

My question are 我的问题是

  1. assume that updateUser will need to start 10 ms after the update address How is it recommended to do so? 假设updateUser将需要在更新地址之后10毫秒开始,建议如何这样做?

  2. in aspects of error handling if one of the process failed (send error message ) I Need to exit ( process.exit(1); ) 在错误处理方面,如果进程之一失败(发送错误消息),我需要退出( process.exit(1);

Use .all : 使用.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

If you really need the 10ms delay, you can do: 如果您确实需要10ms的延迟,则可以执行以下操作:

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

Although I suspect that you really just want updateUser to happen before updateAddress which can be easily solved with: 尽管我怀疑您确实只是希望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

If you need to exit on promise error, you can do: 如果需要退出promise错误,可以执行以下操作:

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

Although I warmly recommend you create meaningful error messages, just a non-zero process exit code is hard to debug. 尽管我热烈建议您创建有意义的错误消息,但是仅非零的进程退出代码很难调试。

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

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