简体   繁体   English

如何从 React 中的 Promise 对象检索数据?

[英]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?但是我以 Promise 对象的形式接收数据,如何从 Promise 中检索数据?

In you example you are combining both of await and .then() , I would use only one of them.在你的例子中,你结合了await.then() ,我只会使用它们中的一个。

Preferably await as the following:最好await如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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