Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 奇怪的承诺链案例:then()参数未定义,未返回最后一个链数据_Javascript_Es6 Promise - Fatal编程技术网

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?