Javascript 奇怪的承诺链案例:then()参数未定义,未返回最后一个链数据
首先,由于以下原因,我无法在此处公开我的原始代码,对此我深表歉意:Javascript 奇怪的承诺链案例:then()参数未定义,未返回最后一个链数据,javascript,es6-promise,Javascript,Es6 Promise,首先,由于以下原因,我无法在此处公开我的原始代码,对此我深表歉意: 太长了 它包含敏感的业务信息 但是,我相信下面的提纲应该足以描述我的承诺链接代码中发生的奇怪情况(请参见代码中的注释): 我想知道这怎么可能发生?我知道then()参数为undefined的唯一原因是前一个then()没有返回任何内容,但我的情况并非如此。另外,我希望将最终then()的结果返回给调用者,而不是第一个调用者。有人能向我指出任何可能导致这种情况发生的可能性吗?提前谢谢 因为上一个then()函数的返回值是返回解析(
then()
参数为undefined
的唯一原因是前一个then()
没有返回任何内容,但我的情况并非如此。另外,我希望将最终then()
的结果返回给调用者,而不是第一个调用者。有人能向我指出任何可能导致这种情况发生的可能性吗?提前谢谢
因为上一个then()
函数的返回值是返回解析(batchResult)
和resolve(…)
函数返回undefined
您的问题是由故障引起的
- 删除
(以及匹配的返回新承诺((解析、拒绝)=>{
});
- 不要调用
,只需返回您要解决承诺的值(resolve
)batchResult
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!
}
}
console.log(prevBatchResult); // prevBatchResult is undefined, why?