的推荐方式
问题描述:
停止无极链执行我有类似这样的代码:的推荐方式
promise_function().then(()=>{
//do something
return another_promise_fucntion();
}).then(() => {
//do something
return another_promise_function1();
}).then((result) => {
//check if result is valid
if(!result)
//break chain (how to stop calling the next .then functions ?)
else
return another_promise_function2();
}).then(()=>{
//do something
return another_promise_function3();
}).catch((err)=>{
//handle error
});
我想停止呼叫下。那么()函数,如果返回的结果是无效的。
我用“抛出新的错误()”,它工作得很好,但我不知道如果这是推荐的方式。
答
这是该重新开始由Mozilla 这里你可以看到更详细的正确方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining
+0
不,这是不正确的。我不明白MDN在哪里推荐这种模式? – Bergi
'throw'是正确的做法。 – 2017-04-23 12:25:21
您可以在那里使用'Promise.reject'。 –
@torazaburo不会“拒绝”是做这件事的理想方式吗?只是问问。 –