简体   繁体   中英

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?

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

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