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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With