admin管理员组

文章数量:1417070

I have two promises, one rejected and other resolved. Promise.all is called. It executed the catch block of Promise.all as one of the promises is rejected.

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

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

Am I missing something about promises.

I have two promises, one rejected and other resolved. Promise.all is called. It executed the catch block of Promise.all as one of the promises is rejected.

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

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

Am I missing something about promises.

Share Improve this question asked Jun 24, 2018 at 16:28 Sebin BenjaminSebin Benjamin 1,9083 gold badges26 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I see that its answer but I think I can clarify a bit more.

Please remember that each then() or catch() return a Promise. (If you don't have any explicit return in callback, both will return Promise.resolve(undefined)). Therefore after the promise has resolved, the value of entire promise chain will be the promise returned by last then(); 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(). The only difference is that its called on rejected promises rather then resolved. In following example, I just replace all resolve by reject to demonstrate that.

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

Now ing to your question. Value of Promise.all() is a rejected promise, since one of the promise in array is rejected. If you have a catch block in chain, control will go to that catch block which will return a 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.

That's why, when you omit the catch, your promise state is "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:

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; } block

本文标签: