javascript - 异步 promise 返回未定义或区域感知 promise

标签 javascript typescript promise angular-promise angular8

当调用返回 promise 的函数时,返回为未定义,除非异步运算符被删除,然后返回 ZoneAwarePromise,但不包含任何数据。

我知道查询会在函数执行时返回数据,但它似乎没有将该数据传递给函数调用的实际返回部分。

我看了几个没有回答这个问题的 Stack 问题,包括这个问题: Async/Await with Request-Promise returns Undefined

这是使用 REST 端点来提取数据,console.logs 确实显示数据是正确的,但是返回未定义

     this.allPeople.forEach(async person => {
          const dodString = await this.getRelatedRecords(person); //undefined
    }

这是返回 promise /数据的主要功能

async getRelatedRecords(person) {
    // function truncated for clarity
    // ...
    //
    console.warn('This async should fire first');
    selPeopleTable.relationships.forEach(relationship => {
    allRelationshipQueries.push(
      arcgisService.getRelatedTableData(
        selPeopleTable.url, [person[oidField.name]], relationship.id, relationship.name),
      );
    });
    await Promise.all(allRelationshipQueries).then(allResults => {
      console.log('Inside the Promise');
      // The Specific node I am looking for
      const data = allResults[1].results.relatedRecordGroups[0].relatedRecords[0].attributes.dod;
    console.log(data); // Shows correctly as the data I am looking for  
    return data;
    }).catch(function(data){
      console.log('there might be data missing', data);
    });
  }

删除 ASYNC 运算符会导致 getRelatedRecords() 在包含函数之后触发和/或返回不包含任何数据的“ZoneAwarePromise”。我需要先触发 getRelatedRecords(),然后运行其余代码。

如果需要,我可以提供更多片段。

区域感知 promise Zone Aware Promise

当异步运算符(我认为)设置正确时 enter image description here

最佳答案

你也需要返回这个:

await Promise.all(allRelationshipQueries).then(allResults => {
    console.log('Inside the Promise');
    // The Specific node I am looking for
    const data = allResults[1].results.relatedRecordGroups[0].relatedRecords[0].attributes.dod;
    console.log(data); // Shows correctly as the data I am looking for  
    return data;
})
上面 block 中的

return 正在返回,但所有这些都在箭头函数的范围内,即 then(allResults => { 所以你还需要 return这个函数是这样的:

返回等待 Promise.all(allRelationshipQueries).then(allResults => {

方法二: 第二种方法是将其存储到这样的变量中:

let dataToReturn = await Promise.all(allRelationshipQueries).then(allResults => {
    console.log('Inside the Promise');
      // The Specific node I am looking for
    const data = allResults[1].results.relatedRecordGroups[0].relatedRecords[0].attributes.dod;
    console.log(data); // Shows correctly as the data I am looking for  
    return data;
    }).catch(function(data){
      console.log('there might be data missing', data);
    });
return dataToReturn;

关于javascript - 异步 promise 返回未定义或区域感知 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58511972/

相关文章:

javascript - 在 React 中使用 Vidyard 嵌入式播放器

javascript - RxJs 管道和可出租运算符 `map` : 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<{}>'

javascript - ngx-bootstrap BsModal 从子组件关闭

angular - 在 Protractor 中模拟日期

node.js - Mongoose 和 promise : how to get an array of query results?

javascript - 如何组合 Promise

javascript - 如何使用 JavaScript 获取浏览器 Cookie

javascript - 使用 jQuery 在框架中获取嵌入 ID

javascript - 更新可视化背后的数据

javascript - 尝试 - catch() 不捕获错误而是抛出错误