I am trying to understand the Fail-fast behavior of Promises.all. Consider the below example;
let p1 = new Promise(function(resolve, reject) {
setTimeout(
function(){
console.log("p1");
resolve('p1');
}, 500);
});
let p2 = new Promise(function(resolve, reject) {
setTimeout(
function(){
console.log("p2");
resolve('p2');
}, 1000);
});
let p3 = new Promise(function(resolve, reject) {
setTimeout(
function(){
console.log("p3");
resolve('p3');
}, 1200);
});
let p4 = new Promise(function(resolve, reject) {
setTimeout(
function(){
console.log("p4");
reject('p4');
}, 600);
});
let p5 = new Promise(function(resolve, reject) {
setTimeout(
function(){
console.log("p5");
resolve('p5');
}, 800);
});
let promise = Promise.all([p1, p2, p3, p4, p5]);
promise
.then(function(data) {
data.forEach(function(data) {
cconsole.log(data);
});
})
.catch(function(error) {
console.error('error is', error);
});
Running the above example does log p1, p2, p3, p4, p5 and then logs "error is p4"
Now, what I read about Promise.all is this;
Promise.all has a fail-fast behaviour. If a given promise is rejected, the resulting promise of Promise.all will be rejected at this exact moment. It will not wait for the other promises to complete
So my question is why/how does it log p1, p2, p3, p4, p5
I was thinking it should have just logged p1 (since that is less than 600 ms at which we have a reject) and then just log "error is p4"
Where might my understanding be wrong?