javascript - 异步函数返回 [object Promise] 而不是实际值
问题描述
我对 ReactJS 很陌生。
我对异步函数的返回值有一些问题
当我打电话时
const result = this.getFieldsAPI();
结果值为[object Promise]
我从console.log("result : " + result);看到 [object Promise]
getFieldsAPI = async() => {
let currentChromosome = "";
switch (this.state.chromosome) {
case "Autosom":
currentChromosome = "/getlocusautosomalkit/";
break;
case "Y_STRs":
currentChromosome = "/getlocusykit/";
break;
case "X_STRs":
currentChromosome = "/getlocusxkit/";
break;
default:
currentChromosome = "";
}
let result = [];
await Axios.get(API_URL + currentChromosome + this.state.currentKit).then((Response) => {
Response.data.map((locus) => {
result.push(locus);
});
})
return "result";
}
// To generate mock Form.Item
getFields() {
const count = this.state.expand ? 10 : 6;
const { getFieldDecorator } = this.props.form;
const children = [];
const result = this.getFieldsAPI();
console.log("result : " + result);
for (let i = 0; i < 10; i++) {
children.push(
<Col span={8} key={i} style={{ display: i < count ? 'block' : 'none' }}>
<Form.Item label={`Field ${i}`}>
{getFieldDecorator(`field-${i}`, {
rules: [{
required: true,
message: 'Input something!',
}],
})(
<Input placeholder="placeholder" />
)}
</Form.Item>
</Col>
);
}
return children;
}
解决方案
你不是在等待的价值,result
所以你只是得到一个未兑现的承诺。如果你改变
const result = this.getFieldsAPI();
至
const result = await this.getFieldsAPI();
你会得到你所追求的。您还需要进行getFields()
异步操作。
推荐阅读
- r - R中具有重复输入的随机匹配data.table
- android - 恢复活动时,在表面视图中捕获的静止图像被实时摄像机重置
- c# - .NET 了解其他进程的内存/从其他进程读取变量
- apache-spark - 如何在 Databricks 中抛出异常?
- python - 计算两条曲线到下一个交点的面积
- arrays - 如何使用 SDCC 创建位域的打包数组?
- android - 向量操作较多时,Android原生c++程序睡眠时间过长
- javascript - 根据一个数组的值过滤具有多个数组的对象
- javascript - 正则表达式:整数或小数或小数
- c# - 编辑 openapi mustache 模板以在方法签名中包含一个附加参数