简体   繁体   English

已执行但未抛出承诺(未处理的承诺拒绝)

[英]Promise Rejection Executed but Not Thrown (Unhandled Promise Rejection)

I'm brand new submitting questions. 我是新提交的问题。 I've had a good look at all the other questions that have been submitted and struggling to find a similar issue to what I have. 我已经很好地查看了所有其他已提交的问题,并且努力寻找与我所遇到的问题类似的问题。 If there is a similar question, my apologies. 如有类似问题,我深表歉意。

I've having issues with a very basic Promise I created for this question, where I need to reject a array loop where one iteration is the same: 我为此问题创建的一个非常基本的Promise遇到了问题,我需要拒绝一次迭代相同的数组循环:

var array = [1, 2, 3, 4, 5];
var promise = array.forEach(n => {
  new Promise((resolve, reject) => {
    if (n == 5) return reject();
    return resolve();
  });
});

Promise.all(promise).then(() => {
  console.log('true');
}).catch(() => {
  console.log('false');
});

When console logging each iteration, the rejection gets called but doesn't catch in the promise and I can't fathom why. 当控制台记录每次迭代的控制台时,拒绝会被调用,但不会实现承诺,我无法理解为什么。 I've spent almost 3 hours finding out the reasoning behind this. 我花了将近3个小时来找出背后的原因。

Array.prototype.forEach does not return anything, thus promise is undefined . Array.prototype.forEach不返回任何内容,因此promiseundefined What you should use instead is Array.prototype.map . 您应该改用Array.prototype.map Also note that you actually have to return the newly created promises: 另请注意,您实际上必须返回新创建的Promise:

var array = [1, 2, 3, 4, 5];
var promise = array.map(n => {
  return new Promise((resolve, reject) => {
    if (n == 5) return reject();
    return resolve();
  });
});

Promise.all(promise).then(() => {
  console.log('true');
}).catch(() => {
  console.log('false');
});

You could even write it like this: 您甚至可以这样写:

var promise = array.map(n => new Promise((resolve, reject) => (n == 5) ? reject() : resolve()));

Whenever you catch an error you should properly log it, and not just log false or something like that. 每当您发现错误时,都应该正确记录它,而不仅仅是记录false或类似的东西。

So if you write console.error(err); 因此,如果您编写console.error(err); instead of console.log('false') , then you would get the error that's something like TypeError: "can't convert undefined to object" or TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined for the line with the Promise.all(promise) . 而不是console.log('false') ,那么您将得到类似TypeError: "can't convert undefined to object"的错误TypeError: "can't convert undefined to object"TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined该行TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefinedPromise.all(promise) And that should give you the hint that promise does not hold an array with Promises but undefined . 这应该给您提示, promise不会保存Promises数组,而是undefined

The promise is undefined because forEach does not return anything. 由于forEach不返回任何内容,因此undefined promise What you are looking for is map . 您正在寻找的是map

The curly bracket around your new Promise((resolve, reject) => {}) prevent that the new Promise is returned, so either add a return in front of the new Promise or remove those curly bracket. new Promise((resolve, reject) => {})周围的大括号可防止返回new Promise ,因此可以在new Promise前面添加一个return或删除那些大括号。

And you should always reject with an actual error, otherwise you won't be able to get a proper stack trace for the error. 而且您应该始终拒绝实际错误,否则您将无法为该错误获得正确的堆栈跟踪。

 var array = [1, 2, 3, 4, 5]; var promise = array.map(n => new Promise((resolve, reject) => { if (n == 5) return reject(new Error('rejected for n==5')); return resolve(); }) ) Promise.all(promise).then(() => { console.log('true'); }).catch(err => { console.error(err); }); 

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

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