Stopping JavaScript Promises from Propagating

我只是一个虾纸丫 提交于 2019-12-08 03:13:36

问题


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)

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.

I am using Angular $q.


回答1:


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


来源:https://stackoverflow.com/questions/27218787/stopping-javascript-promises-from-propagating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!