Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Zone Aware Promise

When the Async operators are (I think) setup correctly enter image description here

like image 585
DSMTurboAWD Avatar asked Oct 28 '22 06:10

DSMTurboAWD


1 Answers

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;
like image 179
Muhammad Usman Avatar answered Nov 11 '22 15:11

Muhammad Usman