如何使这些功能按顺序运行

时间:2020-11-03 17:31:40

标签: javascript reactjs

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

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())
            ;
    }

0 个答案:

没有答案