gpt4 book ai didi

JavaScript 如何等到函数及其所有子函数完成

转载 作者:行者123 更新时间:2023-11-29 10:29:47 25 4
gpt4 key购买 nike

在我的 reactJS 应用程序中,我有以下带有 onClick

的按钮
<Button
onClick={() => {
this.authenticateUser(this.state.user, this.state.password)
.then(user => {
if (user === "true") {
this.setState({ markAsDone: true }, () => {
this.startStepInstancePostConditions(this.props.step, this.props.instance.id);
this.markSepInstanceAsDone(this.props.step, this.props.instance.id);
});
this.toggleDialog();
}
})
.catch(() => {
this.snackBarHandler(this.toggleSnackBar, "", t("auditLogs:newAuditLog.error"), 5000);
});
}}
>
{t("processes:processStepInstances.markAsDoneDialog.save")}
</Button>

我不确定如何解决我的问题。

两个函数:

this.startStepInstancePostConditions(this.props.step,this.props.instance.id);  

this.markSepInstanceAsDone(this.props.step,this.props.instance.id);

遍历一些数组并执行一些 axios 帖子。

我希望我的应用程序调用它:

 this.props.history.push("/processes/processInstance/" + this.props.instance.id);

如果上面两个函数和所有的子函数都做完了。我怎样才能做到这一点?

提前致谢

更新:

export function markSepInstanceAsDone(step, processInstanceId) {
const user = sessionStorage.getItem("username");
const clientId = sessionStorage.getItem("client");
axios.post(PROCESS_INSTANCES_URL + '/markProcessStepInstanceAsDone/' + clientId, {
stepInstanceId: step.id,
user: user,
comment: this.state.markStepInstanceAsDoneComment
})
.then((response) => {
this.getContentComponentPostConditions(step, processInstanceId);
this.props.history.push("/processes/processInstance/" + processInstanceId);
})
.catch((error) => {
this.setState({dialogLoaded: false});
console.log(error);
});
}

更新

export function startStepInstancePostConditions(step,processInstanceID) {
step.postConditions.map(postConditionTemplate => {
axios.get(API_URL + '/processStepTemplates/' + postConditionTemplate.id)
.then(response => {
this.createProcessInstanceAutomatic(postConditionTemplate, response.data);
})
.catch(error => {
console.log(error);
});
});
}

最佳答案

让你的两个函数从它们的 axios 调用中返回 promise ,然后你可以使用 Promise.all等到他们都完成了,然后再做一些事情。

startStepInstancePostConditions() {
return axios.post('...')
}
markSepInstanceAsDone() {
return axios.post('...')
}

// inside the authenticateUser callback
Promise.all([
this.startStepInstancePostConditions(this.props.step, this.props.instance.id),
this.markSepInstanceAsDone(this.props.step, this.props.instance.id)
]).then(() => {
this.props.history.push("/processes/processInstance/" + this.props.instance.id)
})

关于JavaScript 如何等到函数及其所有子函数完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48839237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com