简体   繁体   English

Promise.all()被拒绝后的值,显示[''PromiseStatus'']:如果存在catch块则解决

[英]Value of Promise.all() after it is rejected, shows [''PromiseStatus'']: resolved if catch block is present

I have two promises, one rejected and other resolved. 我有两个承诺,一个拒绝,另一个解决。 Promise.all is called. Promise.all被称为。 It executed the catch block of Promise.all as one of the promises is rejected. 它执行Promise.all的catch块,因为其中一个承诺被拒绝。

const promise1 = Promise.resolve('Promise 1 Resolved');
const promise2 = Promise.reject('Promise 2 Rejected');

const promise3 = Promise.all([promise1, promise2])
  .then(data => {
    console.log('Promise.all Resolved', data);
  })
  .catch(error => {
    console.log('Promise.all REJECTED', error);
  })
setTimeout(() => {
  console.log(promise1, promise2, promise3)
}, 200);

在此输入图像描述

If I don't have the catch on Promise.all(), the value remains as Rejected, ie 如果我没有Promise.all()上的catch,则值仍为Rejected,即

const promise3 = Promise.all([promise1, promise2])
  .then(data => {
    console.log('Promise.all Resolved', data);
  })

Am I missing something about promises. 我错过了一些关于承诺的事情。

I see that its answer but I think I can clarify a bit more. 我看到它的答案,但我想我可以澄清一点。

Please remember that each then() or catch() return a Promise . 请记住每个then()catch()返回一个Promise (If you don't have any explicit return in callback, both will return Promise.resolve(undefined) ). (如果在回调中没有任何显式return ,则两者都将返回Promise.resolve(undefined) )。 Therefore after the promise has resolved, the value of entire promise chain will be the promise returned by last then() ; 因此,在promise解决后,整个promise链的值将是last then()返回的promise; Example: 例:

promise = Promise.resolve(1)
    .then(() => Promise.resolve(2))
    .then(() => Promise.resolve(3));
console.log(promise);
setTimeout(() => {
    console.log(promise)//Promise {<resolved>: 3}
}, 0)

catch() works in exactly like then() . catch()工作方式与then()完全相同。 The only difference is that its called on rejected promises rather then resolved . 唯一的区别是它呼吁rejected承诺而不是resolved In following example, I just replace all resolve by reject to demonstrate that. 在下面的示例中,我只是通过reject替换所有resolve来演示。

promise = Promise.reject(1)
    .catch(() => Promise.reject(2))
    .catch(() => Promise.reject(3));
console.log(promise);
setTimeout(() => {
    console.log(promise)//Promise {<rejectd>: 3}
}, 0)

Now coming to your question. 现在回答你的问题。 Value of Promise.all() is a rejected promise, since one of the promise in array is rejected. Promise.all()值是被拒绝的承诺,因为数组中的一个承诺被拒绝。 If you have a catch block in chain, control will go to that catch block which will return a Promise.resolve(undefined) . 如果你有一个catch块,控件将转到catch块,它将返回一个Promise.resolve(undefined) If you have no catch block in the chain, you will get what you have: a rejected promise. 如果链条中没有任何阻塞块,您将得到您拥有的东西:被拒绝的承诺。

A catch on a Promise acts the same as a try {} catch {} block, in that you have captured the error state and the program will continue to function as normal. 对Promise的catchtry {} catch {}块的作用相同,因为您已捕获错误状态,程序将继续正常运行。

That's why, when you omit the catch , your promise state is "rejected" . 这就是为什么,当你省略catch ,你的承诺状态被"rejected"

If, after having caught the error, you want to return the promise state as rejected you need to return a rejected promise from the catch handler: 如果在捕获错误后,您希望将promise状态返回为已拒绝,则需要从catch处理程序返回被拒绝的promise:

const promise3 = Promise.all([promise1, promise2])
    .catch(error => {
        console.log("REJECTED", error);
        return Promise.reject(error);
    });
console.log(promise3); // [[PromiseStatus]]: "rejected"

Similar to doing a throw inside a try {} catch { throw; } 类似于做一个throw一个内部try {} catch { throw; } try {} catch { throw; } block try {} catch { throw; }

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

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