根据参数不同,使用Promise.all 方法等待全部执行完

const getStreamingatientData = async () => {
    // 创建新的AbortController实例
    cancelPendingRequests();
    abortController = new AbortController();
    
    // 过滤掉无效的参数
    const validParams = paramss.filter(item => {
        return item.userId && item.patientId;
    });
    
    if (validParams.length === 0) {
        console.log('No valid parameters for streaming data');
        return;
    }
    // 使用Promise.all并行执行所有请求,同时在每个请求完成后立即处理响应数据
    const promises = validParams.map(async (item) => {
        console.log('Processing request for type:', item.type);
        
        try {
            const res = await PatientAPI.getPatientDataByworkbenchStream(item, abortController.signal);
            console.log('Request completed:', res);
            // 处理响应数据(即返回即处理)
            if (res.code === '200' && res.data) {
                // 诊疗建议
                if (res.data.type === 'medical_advice') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }
                    aiLoading.value[0] = false
                    patientInfo.value.medicalRecordAdvice.visitAdvice = {content:res.data};
                }
                // 用药建议
                if (res.data.type === 'medication_instructions') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }

                    let data = cloneDeep(res)
                    if(!res.data){
                        aiLoading.value[1] = false
                        return
                    }
                    data.content = safeJsonParse(res.data)
                    if(data.content && data.content.data){
                        data.content.data.map((item,index) => {
                            item.undosage = item.dosage + '/' + item.dosageUnit
                        })
                    }
                    aiLoading.value[1] = false
                    patientInfo.value.medicalRecordAdvice.durgUsages = data;
                }
                // 病历优化建议
                if (res.data.type === 'medical_record_optimization') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }
                    aiLoading.value[2] = false
                    patientInfo.value.medicalRecordAdvice.medicalRecordOptimizationAdvice = {content:res.data};
                }
                // 患者风险
                if (res.data.type === 'patient_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[3] = false
                    patientInfo.value.riskAlert.patientRisk = {content:res.data};
                }
                // 处方风险 PRESCRIPTION_RISK
                if (res.data.type === 'prescription_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    if(!res.data){
                        return
                    }
                    let data = cloneDeep(res.data);
                   data.data = transformPrescriptionRisk(data.data)
                    aiLoading.value[4] = false
                    patientInfo.value.riskAlert.prescriptionRisk = {content:data.data};
                    console.log("patientInfo.value.riskAlert.prescriptionRisk",patientInfo.value);
                }
                // 其他建议 OTHER_MEDICAL_ADVICE_RISK
                if (res.data.type === 'other_medical_advice_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[5] = false
                    patientInfo.value.riskAlert.otherAdviceRisk = {content:res.data};
                }
                // 病历质量控制风险 MEDICAL_RECORD_QUALITY_CONTROL_RISK
                if (res.data.type === 'medical_record_quality_control_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[6] = false
                    patientInfo.value.riskAlert.medicalRecordQualityTips = {content:res.data};
                }
                // 保险拒绝风险 INSURANCE_REFUSE_RISK
                if (res.data.type === 'insurance_refuse_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[7] = false
                    patientInfo.value.riskAlert.refusePayRisk = {content:res.data};
                }
                // // 相似病历 SIMILAR_CASES
                if (res.data.type === 'similar_cases') {
                    const data = cloneDeep(res.data);
                    if (data.similarCases && data.similarCases.length > 0) {
                        data.similarCases.forEach((item,index) => {
                            item.index = index + 1
                        })
                    }
                    aiLoading.value[8] = false
                    if(data.similarCases.length > 0 && chat.sideList.includes('相似病历') === false){
                        chat.setSideList([...chat.sideList,'相似病历'])
                    }
                    patientInfo.value.similarCase  = {
                        content:data
                    }
                }
                // 人群健康指导 POPULATION_HEALTH_GUIDANCE
                if (res.data.type === 'population_health_guidance') {
                    const data = res.data.data;

                    const hasValidChildTag = data && data != null && data.childTag &&  data.childTag !== null &&
                                        typeof data.childTag === 'object' && 
                                        !isEmpty(data.childTag) &&
                                        Object.values(data.childTag).some(hasValidValue);
                    
                    const hasValidChronicDiseaseTag =  data && data != null && data.chronicDiseaseTag &&  data.chronicDiseaseTag !== null &&
                                                typeof data.chronicDiseaseTag === 'object' && 
                                                !isEmpty(data.chronicDiseaseTag) &&
                                                Object.values(data.chronicDiseaseTag).some(hasValidValue);
                    aiLoading.value[9] = false
                    if((hasValidChildTag || hasValidChronicDiseaseTag) && chat.sideList.includes('人群健康指导') === false){
                        chat.setSideList([...chat.sideList,'人群健康指导'])
                    }
                    patientInfo.value.healthGuidance = {'content':{data:data}};
                }
            }
            
            return res;
        } catch (error) {
            console.log('Error processing request for type', item.type, ':', error);
            // 忽略请求取消的错误
            if (error.name !== 'AbortError') {
                console.error('Error processing request for type', item.type, ':', error);
            }
            // 返回错误信息,以便后续处理
            return { error: true, type: item.type, message: error.message };
        }
    });
    
    // 等待所有请求完成
    const results = await Promise.all(promises);
    console.log('All requests completed', results);
    // 检查是否有 409 状态码的返回值
    const has409 = results.some(result => result.code === '409');
    
    if (has409) {
        Patient409.value = true
        // 处理 409 状态码
        console.log('Found 409 status code, processing...');
        
        // 过滤出 409 状态码的结果
        const error409Results = results.filter(result => result.code === '409');
        
        // 提取需要过滤的 type 值
        const invalidTypes = new Set();
        error409Results.forEach(result => {
            if (result.message) {
                // 假设 message 中包含 type 信息,或者直接使用 result.data.type
                // 这里需要根据实际返回格式调整
                const type = result.data?.type || result.type;
                if (type) {
                    invalidTypes.add(type);
                }
            }
        });
        
        // 过滤 paramss,去除 type 值在 invalidTypes 中的项
        if (invalidTypes.size > 0) {
            console.log('Filtering out invalid types:', Array.from(invalidTypes));
            paramss = paramss.filter(item => !invalidTypes.has(item.type));
        }
        
        // 定时循环调用 getPatientData 方法
        console.log('Scheduling next getPatientData call...');

        patientDataTimer = setInterval(() => {
            getPatientData();
        }, 5000);

    } else {
        // 所有请求都成功,设置 assistantModeData
        console.log('patientInfo.value44444444', patientInfo.value);
        chat.setAssistantModeData(patientInfo.value);
    }
}

代码优化,需要每次执行3个请求,且要保留执行完成后的方法

const getStreamingatientData = async () => {
    // 创建新的AbortController实例
    cancelPendingRequests();
    abortController = new AbortController();
    
    // 过滤掉无效的参数
    const validParams = paramss.filter(item => {
        return item.userId && item.patientId;
    });
    
    if (validParams.length === 0) {
        console.log('No valid parameters for streaming data');
        return;
    }
    
    // 并发控制函数,每次执行3个请求,完成一个后立即补上一个
    const concurrencyLimit = 3;
    const results = [];
    const paramsQueue = [...validParams];
    
    // 处理单个请求的函数
    const processRequest = async (item) => {
        console.log('Processing request for type:', item.type);
        
        try {
            const res = await PatientAPI.getPatientDataByworkbenchStream(item, abortController.signal);
            console.log('Request completed:', res);
            // 处理响应数据(即返回即处理)
            if (res.code === '200' && res.data) {
                // 诊疗建议
                if (res.data.type === 'medical_advice') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }
                    aiLoading.value[0] = false
                    patientInfo.value.medicalRecordAdvice.visitAdvice = {content:res.data};
                }
                // 用药建议
                if (res.data.type === 'medication_instructions') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }

                    let data = cloneDeep(res)
                    if(!res.data){
                        aiLoading.value[1] = false
                        return
                    }
                    data.content = safeJsonParse(res.data)
                    if(data.content && data.content.data){
                        data.content.data.map((item,index) => {
                            item.undosage = item.dosage + '/' + item.dosageUnit
                        })
                    }
                    aiLoading.value[1] = false
                    patientInfo.value.medicalRecordAdvice.durgUsages = data;
                }
                // 病历优化建议
                if (res.data.type === 'medical_record_optimization') {
                    if (!patientInfo.value.medicalRecordAdvice) {
                        patientInfo.value.medicalRecordAdvice = {};
                    }
                    aiLoading.value[2] = false
                    patientInfo.value.medicalRecordAdvice.medicalRecordOptimizationAdvice = {content:res.data};
                }
                // 患者风险
                if (res.data.type === 'patient_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[3] = false
                    patientInfo.value.riskAlert.patientRisk = {content:res.data};
                }
                // 处方风险 PRESCRIPTION_RISK
                if (res.data.type === 'prescription_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    if(!res.data){
                        return
                    }
                    let data = cloneDeep(res.data);
                   data.data = transformPrescriptionRisk(data.data)
                    aiLoading.value[4] = false
                    patientInfo.value.riskAlert.prescriptionRisk = {content:data.data};
                    console.log("patientInfo.value.riskAlert.prescriptionRisk",patientInfo.value);
                }
                // 其他建议 OTHER_MEDICAL_ADVICE_RISK
                if (res.data.type === 'other_medical_advice_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[5] = false
                    patientInfo.value.riskAlert.otherAdviceRisk = {content:res.data};
                }
                // 病历质量控制风险 MEDICAL_RECORD_QUALITY_CONTROL_RISK
                if (res.data.type === 'medical_record_quality_control_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[6] = false
                    patientInfo.value.riskAlert.medicalRecordQualityTips = {content:res.data};
                }
                // 保险拒绝风险 INSURANCE_REFUSE_RISK
                if (res.data.type === 'insurance_refuse_risk') {
                    if (!patientInfo.value.riskAlert) {
                        patientInfo.value.riskAlert = {};
                    }
                    aiLoading.value[7] = false
                    patientInfo.value.riskAlert.refusePayRisk = {content:res.data};
                }
                // // 相似病历 SIMILAR_CASES
                if (res.data.type === 'similar_cases') {
                    const data = cloneDeep(res.data);
                    if (data.similarCases && data.similarCases.length > 0) {
                        data.similarCases.forEach((item,index) => {
                            item.index = index + 1
                        })
                    }
                    aiLoading.value[8] = false
                    if(data.similarCases.length > 0 && chat.sideList.includes('相似病历') === false){
                        chat.setSideList([...chat.sideList,'相似病历'])
                    }
                    patientInfo.value.similarCase  = {
                        content:data
                    }
                }
                // 人群健康指导 POPULATION_HEALTH_GUIDANCE
                if (res.data.type === 'population_health_guidance') {
                    const data = res.data.data;

                    const hasValidChildTag = data && data != null && data.childTag &&  data.childTag !== null &&
                                        typeof data.childTag === 'object' && 
                                        !isEmpty(data.childTag) &&
                                        Object.values(data.childTag).some(hasValidValue);
                    
                    const hasValidChronicDiseaseTag =  data && data != null && data.chronicDiseaseTag &&  data.chronicDiseaseTag !== null &&
                                                typeof data.chronicDiseaseTag === 'object' && 
                                                !isEmpty(data.chronicDiseaseTag) &&
                                                Object.values(data.chronicDiseaseTag).some(hasValidValue);
                    aiLoading.value[9] = false
                    if((hasValidChildTag || hasValidChronicDiseaseTag) && chat.sideList.includes('人群健康指导') === false){
                        chat.setSideList([...chat.sideList,'人群健康指导'])
                    }
                    patientInfo.value.healthGuidance = {'content':{data:data}};
                }
            }
            
            return res;
        } catch (error) {
            console.log('Error processing request for type', item.type, ':', error);
            // 忽略请求取消的错误
            if (error.name !== 'AbortError') {
                console.error('Error processing request for type', item.type, ':', error);
            }
            // 返回错误信息,以便后续处理
            return { error: true, type: item.type, message: error.message };
        }
    };
    
    // 并发执行函数
    const runConcurrent = async () => {
        const runningPromises = [];
        
        // 处理单个请求的函数,递归处理队列
        const handleRequest = (item) => {
            const promise = processRequest(item).then(result => {
                results.push(result);
                // 当一个请求完成后,从运行队列中移除
                const index = runningPromises.indexOf(promise);
                if (index > -1) {
                    runningPromises.splice(index, 1);
                }
                // 如果还有未处理的参数,继续处理
                if (paramsQueue.length > 0) {
                    const nextItem = paramsQueue.shift();
                    const nextPromise = handleRequest(nextItem);
                    runningPromises.push(nextPromise);
                }
            });
            return promise;
        };
        
        // 初始化第一批请求
        while (runningPromises.length < concurrencyLimit && paramsQueue.length > 0) {
            const item = paramsQueue.shift();
            const promise = handleRequest(item);
            runningPromises.push(promise);
        }
        
        // 等待所有正在运行的请求完成
        await Promise.all(runningPromises);
    };
    
    // 执行并发请求
    await runConcurrent();
    
    // 等待所有请求完成
    console.log('All requests completed', results);
    // 检查是否有 409 状态码的返回值
    const has409 = results.some(result => result.code === '409');
    
    if (has409) {
        Patient409.value = true
        // 处理 409 状态码
        console.log('Found 409 status code, processing...');
        
        // 过滤出 409 状态码的结果
        const error409Results = results.filter(result => result.code === '409');
        
        // 提取需要过滤的 type 值
        const invalidTypes = new Set();
        error409Results.forEach(result => {
            if (result.message) {
                // 假设 message 中包含 type 信息,或者直接使用 result.data.type
                // 这里需要根据实际返回格式调整
                const type = result.data?.type || result.type;
                if (type) {
                    invalidTypes.add(type);
                }
            }
        });
        
        // 过滤 paramss,去除 type 值在 invalidTypes 中的项
        if (invalidTypes.size > 0) {
            console.log('Filtering out invalid types:', Array.from(invalidTypes));
            paramss = paramss.filter(item => !invalidTypes.has(item.type));
        }
        
        // 定时循环调用 getPatientData 方法
        console.log('Scheduling next getPatientData call...');

        patientDataTimer = setInterval(() => {
            getPatientData();
        }, 5000);

    } else {
        // 所有请求都成功,设置 assistantModeData
        console.log('patientInfo.value44444444', patientInfo.value);
        chat.setAssistantModeData(patientInfo.value);
    }
}