Search code examples
javascriptecmascript-6promisees6-promise

Promise.all Rejection from API


I have looked through the other stackoverflow answers and cannot seem to find an answer to this.

Effectively I am writing a module that verifies some data and then if there is no error passes it off to a third party api.

My issue is that if the Promise.all rejects, then my initial calling promise is still moving to next.

------my main app---------

const data = [1,0,1]
api.sendData(data)
    .then( () => {
         *ALWAYS HITTING THIS*
     })
    .catch(err => console.log(err))

---------the api---------

return await a.test(data).then(rd => {
    return rd
})
.catch(ed => {
    return ed
});

-----a.test function--------

let request = data.map((i) => {
    return new Promise((resolve, reject) => {
        if(i < 1) {
           reject('value to low')
        }
        resolve(i);
     });
});
    return await Promise.all(data)
});

Could anyone tell me why my catch is not being hit in my main app? The above code is pseudo, but describes the problem.


Solution

  • I think the problem comes from the fact you catch the error in the api function and the catch method return a new promise (fulfilled as you effectively return a value)

    so if you do not catch the error, your problem should be fixed as the error will be forwarded

    return a.test(data).then(rd => {
       // do some stuffs ...
    
       return rd;
    })
    // no catch