简体   繁体   English

打破promise.all on catch

[英]break promise.all on catch

I have a promise implementation like the one on MDN : 我有一个像MDN那样的承诺实现:

  var p1 = new Promise((resolve, reject) => { setTimeout(resolve, 1000, "one"); }); var p2 = new Promise((resolve, reject) => { setTimeout(resolve, 2000, "two"); }); var p3 = new Promise((resolve, reject) => { setTimeout(resolve, 3000, "three"); }); var p4 = new Promise((resolve, reject) => { setTimeout(resolve, 4000, "four"); }); var p5 = new Promise((resolve, reject) => { reject("reject"); }); Promise.all([p1, p2, p3, p4, p5]).then(value => { console.log(value); }, function(reason) { console.log(reason) }); 

This does fast error-catching: 这样可以快速捕获错误:

Promise.all is rejected if one of the elements is rejected and Promise.all fails fast: If you have four promises which resolve after a timeout , and one calls reject immediately, then Promise.all rejects immediately. Promise.all如果一个元素被拒绝,被拒绝, Promise.all失败快:如果你有四个承诺其解决后timeout ,和一个呼叫reject立即,然后Promise.all立即拒绝。

However, the functions inside a promise don't stop running. 但是, promise的函数不会停止运行。 What I want, is that if one function does a reject , the other functions stop running (preventing useless messages to a user). 我想要的是,如果一个函数执行reject ,则其他函数停止运行(防止向用户发送无用的消息)。

Example of a promise that keeps running after the reject: 在拒绝后继续运行的承诺示例:

 var p1 = new Promise((resolve, reject) => { setTimeout(resolve, 1000, "one"); }); var p2 = new Promise((resolve, reject) => { setTimeout(resolve, 2000, "two"); }); var p3 = new Promise((resolve, reject) => { setTimeout(resolve, 3000, "three"); }); var p4 = new Promise((resolve, reject) => { setTimeout(() => { console.log('Don\\'t show this after a reject!'); resolve(); }, 4000); }); var p5 = new Promise((resolve, reject) => { reject("reject"); }); Promise.all([p1, p2, p3, p4, p5]).then(value => { console.log(value); }, function(reason) { console.log(reason) }); 

There's nothing general in promises that will do that, since stopping the other actions is action-specific, not generic. 承诺中没有任何通用可以做到这一点,因为停止其他操作是特定于操作的,而不是通用的。 For instance, in your example, you have to keep the timer handle and then use clearTimeout ; 例如,在您的示例中,您必须保留计时器句柄,然后使用clearTimeout ; with an ajax request, you might need to do an abort call; 使用ajax请求,您可能需要执行abort调用; with something else, it would be something else. 与其他东西,它将是另一回事。

So you'll need to handle this in your real code in a way that's specific to your real code, using .catch on the promise returned by Promise.all (or the second arg to then as you have). 因此,您需要在实际代码中以特定于您的实际代码的方式处理此问题,使用.catchPromise.all返回的承诺(或者第二个arg, then就像您所拥有的那样)。

In the specific code in your question, it would look something like this (I've added output to the timers we don't cancel as well), but again it will vary depending on what you're cancelling: 在您问题的具体代码中,它看起来像这样(我已经将输出添加到我们也没有取消的定时器),但是它会根据您取消的内容而有所不同:

 var timersToReject = []; var p1 = new Promise((resolve, reject) => { setTimeout(v => { console.log("resolving " + v); resolve(v); }, 1000, "one"); }); var p2 = new Promise((resolve, reject) => { setTimeout(v => { console.log("resolving " + v); resolve(v); }, 2000, "two"); }); var p3 = new Promise((resolve, reject) => { setTimeout(v => { console.log("resolving " + v); resolve(v); }, 3000, "three"); }); var p4 = new Promise((resolve, reject) => { timersToReject.push(setTimeout(() => { console.log('Don\\'t show this after a reject!'); resolve(); }, 4000)); }); var p5 = new Promise((resolve, reject) => { reject("reject"); }); Promise.all([p1, p2, p3, p4, p5]).then(value => { console.log(value); }, function(reason) { console.log(reason) timersToReject.forEach(t => clearTimeout(t)); }); 

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

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