【发布时间】:2021-03-14 04:40:48
【问题描述】:
我是 React 和 javascript 的新手,需要一些帮助。我正在使用一个返回包含接口的 Promise 的函数。我想访问接口中的变量并将其应用到<dl><dt>{//string variable}</dt></dl>。
问题是我只得到一个 Promise 对象,我需要它作为一个字符串。我该怎么做?
这是我获取 Promise 对象并使用它的异步函数:
async function getName() {
const res = await getNamePromise(); // type: Promise<Interface>
console.log(res.name);
}
export function userInfo(){
return(
<dl>
<dd>{// need a string variable}</dd>
</dl>
);
}
什么时候这样写:
getName().then((name) => console.log(name)); 名称是一个字符串。但是如何将其存储为字符串变量并使用它呢?
【问题讨论】:
标签: javascript reactjs typescript promise async-await