简体   繁体   中英

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.

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.

I have looked at several Stack questions that have not answered this question including this question: 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

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

This is the main function that returns a promise / data

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. I need getRelatedRecords() to fire first, then to run the rest of the code.

I can provide more snippets if need be.

Zone Aware 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 await Promise.all(allRelationshipQueries).then(allResults => {

Approach #2: Second way would be to store that into variable like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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