First of all, I would like to apologize for not being able to expose my original code here, due to the following reasons:
- It's too lengthy.
- It contains sensitive business information.
However, I believe the following outline should be enough to describe something weird happen to my promise chaining code (see the comments in code):
return new Promise((resolve, reject) => {
return somePromiseFunction()
.then(async allAccSummaries => {
// Some very lengthy codes here.
// Contains several await function calls.
let batchResult = {
// Some property assignments here
};
console.log(batchResult); // Confirmed batchResult is NOT undefined.
return resolve(batchResult); // This was the result returned to the caller instead!
})
.then(prevBatchResult => {
console.log(prevBatchResult); // prevBatchResult is undefined, why?
// Some other very lengthy logics here.
let finalBatchResult = {
// Some property assignments here
};
return finalBatchResult; // This was not returned to caller as final result!
}
}
I wonder how can this be possibly happened? I know that the only reason for the then()
parameter to be undefined
is when the previous then()
was not returning anything, but this isn't true for my case. Also, I want the result of the final then()
to be returned to the caller, not the first one. Can somebody point out to me any possibility that can cause this to happen? Thanks in advance!