0

Here is my code snippet for parsing application data:

async function parseApplication(data: Application) {


const fieldGroupValues = {};
  for (const group of Object.keys(data.mappedFieldGroupValues)) {
    const groupValue = data.mappedFieldGroupValues[group];
    for (const fieldName of Object.keys(groupValue.mappedFieldValues)) {
      const { fieldValue } = groupValue.mappedFieldValues[fieldName];
  }
  return fieldGroupValues;
}

But I receive data as Promise object, how can I retrieve data from Promise?

  • You can use `await` keyword to get the result in an `async` function or using `.then()`. You can take a look at this question which probably helps: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – norbitrial Oct 23 '20 at 10:07
  • thank you! it seems like I've used it but it still not working – Chihiro Ogino Oct 23 '20 at 10:11
  • It seems you are combining both, you need to either use `await` or `.then()`. – norbitrial Oct 23 '20 at 10:13

1 Answers1

1

In you example you are combining both of await and .then(), I would use only one of them.

Preferably await as the following:

try {
   const dict = await getDictionaryByKey(fieldValue.value.entityDefinitionCode);
   const dictItem = dict.find((item) => fieldValue.value.entityId === item.code);
   acc[fieldName] = dictItem ? dictItem.text : fieldValue.value.entityId;
} catch (err) {
   acc[fieldName] = fieldValue.value.entityId;
}
norbitrial
  • 14,716
  • 7
  • 32
  • 59