I'm using promise object in node.js. I have a object:
var Send = {
send(post_ids) {
return Post.findById(post_ids)
.then((posts) => {
return sendArticlesToWechat(setupArticles(posts)); // sendArticlesToWechat is also a promise
})
.then((result) => {
console.log("RESULT: " + result);
})
.catch((err) => {
console.error("SEND ERROR: " + err);
return err;
});
},
}
export default Send;
and call its method in another file:
Send.send(req.body)
.then((result) => {
console.log("CALL SEND: " + result);
})
.catch((err) => {
console.error(err);
});
When an error occurs, I got two output:
SEND ERROR: ERROR: // error message
CALL SEND: ERROR: // error message
This error occurred in the sendArticlesToWechat() function which be returned. Because it's a promise too so I can catch its error outside. This is what I expected.
When I call the Send.send(), I expected to get the error in catch(), but the error appears in the then() method.
According to the output, the error did returned from the previous catch(), why I can not keep it in the catch()?