Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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);

enter image description here

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.

like image 801
Sebin Benjamin Avatar asked Jun 24 '18 16:06

Sebin Benjamin


People also ask

What happens when a promise is rejected in promise all?

2) Rejected promises example The Promise. all() returns a Promise that is rejected if any of the input promises are rejected. In this example, we have three promises: the first one is resolved after 1 second, the second is rejected after 2 seconds, and the third one is resolved after 3 seconds.

How do you handle rejected promises in promises?

all is all or nothing. It resolves once all promises in the array resolve, or reject as soon as one of them rejects. In other words, it either resolves with an array of all resolved values, or rejects with a single error. Some libraries have something called Promise.

How do you check if all promises are resolved?

Checking if All Promises are Resolved Successfully all() method can be used to check whether all Promises have fulfilled successfully. It accepts an iterable object (e.g. an array) of multiple Promises and returns a Promise. The returned Promise is resolved if all the input Promises passed to it are resolved.

What does promise all () do?

all() The Promise. all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises.

What happens when a promise gets rejected with an error?

Then, Promise.all () itself as a promise will get resolved once all the ten promises get resolved, or any of the ten promises get rejected with an error. If any of the passed-in promises reject, a Promise.all () function asynchronously rejects the value of a promise that is rejected, independent of what other promises have resolved or not.

How to resolve a promise in JavaScript?

Promise.resolve () method in JS returns a Promise object that is resolved with a given value. Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state.

What happens to the promise if the value is returned?

If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned. Value (s) to be resolved by this Promise. Either the promise of the promise fulfilled with its value is returned.

What is the difference between resolve and reject methods in promise?

The Promise.resolve/reject methods. Promise.resolve(value) – It resolves a promise with the value passed to it. It is the same as the following: let promise = new Promise(resolve => resolve(value)); Promise.reject(error) – It rejects a promise with the error passed to it. It is the same as the following:


1 Answers

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 coming 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.

like image 60
dasfdsa Avatar answered Sep 23 '22 09:09

dasfdsa