我很新于ReactJS。
我遇到了一些关于异步函数返回值的问题。
当我调用时:
const result = this.getFieldsAPI();
这里的result值是[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;
}
const result = await this.getFieldsAPI();
。 - Sirko