if (type === 'INTERNAL') { documentHeader = documentHeader as InternalDocumentHeader;…
- User
if (type === 'INTERNAL') { documentHeader = documentHeader as InternalDocumentHeader;
const { senderList, recipientList, recipientsCount = 0, controllerComment, committee, approvalKind, assessmentProcessId, retellingProcessId, } = documentHeader; this.approvalKind = approvalKind; this.senders = senderList; this.recipients = recipientList; this.recipientsCount = recipientsCount; this.controllerComment = controllerComment ?? ''; this.committee = committee; if (assessmentProcessId) { const { data: { processData }, } = await getProcess({ path: { processId: assessmentProcessId } }); this.documentAssessmentProcessText = processData; } if (retellingProcessId) { const { data: { processData }, } = await getProcess({ path: { processId: retellingProcessId } }); this.documentRetellingText = processData; } } if (type === 'OUTGOING') { documentHeader = documentHeader as OutgoingDocumentHeader; const { senderList, addresseeList = [], addresseesCount = 0, approvalKind, committee, isMedoDocument, retellingProcessId, } = documentHeader; this.isMedoDocument = isMedoDocument; this.senders = senderList; this.approvalKind = approvalKind; this.addresseeList = addresseeList; this.addresseesCount = addresseesCount; this.committee = committee; if (retellingProcessId) { const { data: { processData }, } = await getProcess({ path: { processId: retellingProcessId } }); this.documentRetellingText = processData; } } Как сделать, чтобы await getProcess не повторялся так часто? Assistant
Можно вынести получение processData в отдельную функцию и переиспользовать её. Вот как можно улучшить код:
private async getProcessData(processId: string | null): Promise<string | null> { if (!processId) return null; const { data: { processData }, } = await getProcess({ path: { processId } }); return processData; } // Основной код if (type === 'INTERNAL') { documentHeader = documentHeader as InternalDocumentHeader; const { senderList, recipientList, recipientsCount = 0, controllerComment, committee, approvalKind, assessmentProcessId, retellingProcessId, } = documentHeader; this.approvalKind = approvalKind; this.senders = senderList; this.recipients = recipientList; this.recipientsCount = recipientsCount; this.controllerComment = controllerComment ?? ''; this.committee = committee; this.documentAssessmentProcessText = await this.getProcessData(assessmentProcessId); this.documentRetellingText = await this.getProcessData(retellingProcessId); } if (type === 'OUTGOING') { documentHeader = documentHeader as OutgoingDocumentHeader; const { senderList, addresseeList = [], addresseesCount = 0, approvalKind, committee, isMedoDocument, retellingProcessId, } = documentHeader; this.isMedoDocument = isMedoDocument; this.senders = senderList; this.approvalKind = approvalKind; this.addresseeList = addresseeList; this.addresseesCount = addresseesCount; this.committee = committee; this.documentRetellingText = await this.getProcessData(retellingProcessId); }Преимущества такого подхода:
- Уменьшается дублирование кода
- Логика получения processData инкапсулирована в отдельном методе
- Код становится более читаемым
- Если понадобится изменить логику получения processData, это нужно будет сделать только в одном месте
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.