I'm newer to JavaScript and thought I understood the basics of Promises, but I am definitely missing something here.
The basic idea here is I am chaining a couple of promises together.
promise3 does some initial work and if it succeeds, then we do some more work. The second stage of this chain is a couple of promises, promise1 and promise2. Because promise2 is rejected, the end result I had expected was that Promise.all rejects, and therefore I expected the result in Promise.allSettled to also be rejected.
However, what I am observing is that the first promise in the chain here determines the result of the chain - if it is resolved then the result is resolved, but if it is rejected then result is rejected.
Why is this the case?
const promise1 = Promise.resolve(1);
const promise2 = Promise.reject(2)
const promise3 = Promise.resolve(3);
const promises = [promise1, promise2];
const p = [ promise3.then(Promise.all(promises)) ]
Promise.allSettled(p).
then((results) => results.forEach((result) => console.log(result.status)))
This is because your code is wrong. then expects a callback, but provides a value of Promise.all(...)
The corrected code is
const promise1 = Promise.resolve(1); const promise2 = Promise.reject(2) const promise3 = Promise.resolve(3); const promises = [promise1, promise2]; const p = [ promise3.then(() => Promise.all(promises)) ] Promise.allSettled(p). then((results) => results.forEach((result) => console.log(result.status)))