Javascript 如何使这些函数按顺序运行

Javascript 如何使这些函数按顺序运行,javascript,reactjs,Javascript,Reactjs,我有以下功能。我遇到的问题是,updateInterviewArrays()中有一个用于获取和存储数据的循环,这很有效。但是我需要在它之后调用的updateStates和doScoreMath函数等待,并在它完成后按顺序运行。但我似乎无法做到这一点 componentDidMount() { this.getScoresFromTables(); } getScoresFromTables() { fetch(API_URL + `/intervi

我有以下功能。我遇到的问题是,
updateInterviewArrays()
中有一个用于获取和存储数据的循环,这很有效。但是我需要在它之后调用的
updateStates
doScoreMath
函数等待,并在它完成后按顺序运行。但我似乎无法做到这一点

componentDidMount() {

        this.getScoresFromTables();

    }


getScoresFromTables() {
        fetch(API_URL + `/interviews/${this.props.auditId}`)
            .then((res) => {
                if (!res.ok) {
                    throw new Error();
                }
                return res.json();
            })
            .then((result) => {
                this.setState({completedInterviews: result}, () => this.updateInterviewArrays());
            })
            .catch((error) => {
                console.log(error);
            })
            .then(this.updateStates())
            .catch((error) => {
                console.log(error);
            })
            .then(this.doScoreMath());
  }


    updateInterviewArrays() {
        const totalInterviews = this.state.completedInterviews.length;

        const tableSections = [
            'audit_general',
            'audit_culture',
            'audit_performance',
            'audit_policies',
            'audit_risk',
            'audit_strategy',
            'audit_rewards',
            'audit_workforce'
        ];

        for (let i = 0; i < totalInterviews; i++){
            for (let j = 0; j < 8; j++){
                
                this.grabTableData(tableSections[i], this.state.completedInterviews[j].employee_id);
            }
        }
    }


async grabTableData (tableName, employeeId) {
        await fetch(API_URL + `/responses/${tableName}/${employeeId}`)
            .then((res) => {
                if (!res.ok) {
                    throw new Error();
                }
                return res.json();
            })
            .then((result) => {
                if (tableName === "audit_general") {
                    tableData.audit_general.push(result[0]);
                } else if (tableName === "audit_culture") {
                    tableData.audit_culture.push(result[0]);
                } else if (tableName === "audit_performance") {
                    tableData.audit_performance.push(result[0]);
                } else if (tableName === "audit_policies") {
                    tableData.audit_policies.push(result[0]);
                } else if (tableName === "audit_risk") {
                    tableData.audit_risk.push(result[0]);
                } else if (tableName === "audit_strategy") {
                    tableData.audit_strategy.push(result[0])
                } else if (tableName === "audit_rewards") {
                    tableData.audit_rewards.push(result[0]);
                } else if (tableName === "audit_workforce") {
                    tableData.audit_workforce.push(result[0]);
                }
                console.log(result);
                console.log(tableData);
            })
            .catch((error) => {
                console.log(error);
            })
            // .then(() => this.updateStates())
            // .catch((error) => {
            //     console.log(error);
            // })
            // .then(() => this.doScoreMath())
            ;
    }
componentDidMount(){
这个.getScoresFromTables();
}
getScoresFromTables(){
获取(API_URL+`/interfaces/${this.props.audited}`)
。然后((res)=>{
如果(!res.ok){
抛出新错误();
}
返回res.json();
})
。然后((结果)=>{
this.setState({completedInterviews:result},()=>this.updateInterviewArrays());
})
.catch((错误)=>{
console.log(错误);
})
.then(this.updateStates())
.catch((错误)=>{
console.log(错误);
})
.然后(这个.doScoreMath());
}
updateInterviewArrays(){
const totalInterviews=this.state.completedInterviews.length;
常量表节=[
“审计概述”,
“审计文化”,
“审计绩效”,
“审计政策”,
“审计风险”,
“审计战略”,
“审计奖励”,
“审计团队”
];
for(设i=0;i{
如果(!res.ok){
抛出新错误();
}
返回res.json();
})
。然后((结果)=>{
如果(表名==“审计概述”){
tableData.audit_general.push(结果[0]);
}else if(tableName==“审计文化”){
tableData.audit_culture.push(结果[0]);
}else if(tableName==“审计绩效”){
tableData.audit_performance.push(结果[0]);
}else if(tableName==“审核策略”){
tableData.audit_policies.push(结果[0]);
}否则如果(表名==“审计风险”){
tableData.audit_risk.push(结果[0]);
}else if(tableName==“审计策略”){
tableData.audit\u strategy.push(结果[0])
}else if(表名==“审计奖励”){
tableData.audit_rewards.push(结果[0]);
}else if(tableName==“审计团队”){
tableData.audit_workforce.push(结果[0]);
}
控制台日志(结果);
console.log(tableData);
})
.catch((错误)=>{
console.log(错误);
})
//.然后(()=>this.updateStates())
//.catch((错误)=>{
//console.log(错误);
// })
//.然后(()=>this.doScoreMath())
;
}

从哪里调用
getScoresFromTables()
?发布整个组件(如果它很大,则删除一些不必要的部分),以便我们建议您修改现有组件。如果不查看组件和函数的调用方式,您可能会得到对您来说不可行的建议。所以,试着发布一个简短的、可复制的组件,这样您就可以得到对您的用例可行的建议。我刚刚添加了它,实际上我只是从
componentDidMount()
调用
getScoresFromTables()
,这是发生的第一件事。但是我还添加了我的
grabbabledata()
,因为您可以看到我对
updateStates()
的调用和
doScoreMath()
的注释,从那里可以很好地工作。但每次循环都会调用它们。因此,我没有试图将它们移动到
getScoresFromTables()
,这样它们在最后只会被调用一次。这时我才意识到,在这种情况下,它们不会等到
updateIntervewArrays()
完成。让所有应该等待的函数执行一些快速间隔,比如1ms。。然后在UpdateInterview数组中。。完成后,制作一些全局变量E 1或其他东西。。在等待函数中,如果(globalVar==1){执行我应该执行的操作}