Chihiro Ogino
Chihiro Ogino

Reputation: 57

How can I retrieve the data from Promise object in React?

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?

Upvotes: 0

Views: 1154

Answers (1)

norbitrial
norbitrial

Reputation: 15166

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

Upvotes: 1

Related Questions