简体   繁体   English

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

[英]Async Promise returns undefined or zone aware promise

When calling a function that returns a promise, comes back as undefined unless async operators are removed, then returns ZoneAwarePromise, but contains no data.当调用返回 promise 的 function 时,除非删除异步运算符,否则返回未定义,然后返回 ZoneAwarePromise,但不包含数据。

I know the query returns data when the function executes, it however does not seem to pass that data to the actual return part of the function call.我知道查询在 function 执行时返回数据,但是它似乎没有将该数据传递给 function 调用的实际返回部分。

I have looked at several Stack questions that have not answered this question including this question: Async/Await with Request-Promise returns Undefined我查看了几个没有回答这个问题的堆栈问题,包括这个问题: Async/Await with Request-Promise returns Undefined

This is using a REST endpoint to pull data, the console.logs do show the data is correct, however return comes back as undefined这是使用 REST 端点来提取数据,console.logs 确实显示数据是正确的,但是返回未定义

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

This is the main function that returns a promise / data这是返回 promise / 数据的主要 function

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);
    });
  }

Removing the ASYNC operators cause the getRelatedRecords() to fire after the containing function and / or return a 'ZoneAwarePromise' which contains no data.删除 ASYNC 运算符会导致getRelatedRecords()在包含 function 之后触发和/或返回不包含数据的“ZoneAwarePromise”。 I need getRelatedRecords() to fire first, then to run the rest of the code.我需要先触发getRelatedRecords() ,然后运行代码的 rest。

I can provide more snippets if need be.如果需要,我可以提供更多片段。

Zone Aware Promise区域感知 Promise 区域感知承诺

When the Async operators are (I think) setup correctly当异步操作符(我认为)正确设置时在此处输入图像描述

You need to return this as well:您还需要返回这个:

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;
})

return in the above block is returning but all of this is in the scope of the arrow function which is then(allResults => { so you also need to return this function like this:上述块中的return正在返回,但所有这些都在箭头 function 的 scope 中,即then(allResults => {所以你还需要像这样返回这个 ZC1C425268E68385D1AB5074C17A94F:

return await Promise.all(allRelationshipQueries).then(allResults => {

Approach #2: Second way would be to store that into variable like this:方法#2:第二种方法是将其存储到变量中,如下所示:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM