由于getValue 是一个async 函数,它返回一个Promise 对象,并且您希望在getValue 执行完成后执行其他函数,您可以在@ 上使用.then() 或await 987654326@。
使用.then()。
submit = () => {
this.props.getValue().then(res=>{
this.props.toggle(); //All these functions execute only after getValue has completed it's execution.
this.notify();
this.props.resetValidation();
this.props.disable();
this.props.actionCost();
})
};
使用等待
submit = async() => {
await this.props.getValue(); //Note that this should be placed on top as this is the function you want to run first, and other functions to execute only after this has completed.
this.props.toggle();
this.notify();
this.props.resetValidation();
this.props.disable();
this.props.actionCost();
};
由于您的 getValues 函数使用 axios 函数,因此您应该在 axios 完成操作后返回 promise。你的getValues 应该是这样的:
使getValues 成为async 函数
const getValues = async() =>{
let res = await axios.get('URL') //Just an instance, change the axios method(GET,POST,PUT) according to your needs.
return res
}
(或)
从getValues返回一个承诺。
const getValues = () =>{
return new Promise((resolve,reject)=>{
axios.get('URL',response=>{
resolve("AXIOS REQUEST COMPLETE")
}
})
}
您可以将您的getValues 更新为上述任何一种方式,并如前所示调用getValues,它将按预期工作。