阻止JavaScript承诺的传播 [英] Stopping JavaScript Promises from Propagating

查看:58
本文介绍了阻止JavaScript承诺的传播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有一个像下面的承诺链.如果调用func2,我希望避免完全调用func3或func4.

Imagine I have a promise chain like the one below. If func2 is called, I would like to avoid func3 or func4 from being called altogether.

AsyncFunction()
.then(func1, func2)
.then(func3, func4)

此刻,如果我在func2中抛出错误,将调用func4.如果我在func2中返回一个值,则似乎调用了func3.

At the moment, If I throw an error in func2, func4 would be called. If I return a value in func2, func3 seems to be called.

我正在使用Angular $ q.

I am using Angular $q.

推荐答案

将嵌套用于分支控制流.在您的情况下,它看起来像这样:

Use nesting for branching control flow. In your case, it would look like this:

AsyncFunction().then(function(res) {
    return func1(res).then(func3, func4);
}, func2);

这篇关于阻止JavaScript承诺的传播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆