Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
Tratamiento de errores en la función recursiva

Tengo una función que copia colecciones de archivos, así que la hice recursiva, primero la llamo con el nombre de la colección para decirlo, después de eso, llamo a la función con cada archivo secundario de esa colección.

Mi problema es que tengo que actualizar el estado de la operación al final (éxito o error), y al ser una función recursiva, es un poco difícil de entender si lo estoy haciendo bien.

Mi solución en este momento se ve así:

 copyDocument = async (sourceId: string, destinationId: string, commandId: string = ''): Promise<void> => { try { //some code to get the entry type based on the id ... if (entry.type === EntryType.COLLECTION) { const children: Entries[] = await this.repo.getEntries(entry.id); //calling the copyDocument function without the commandId const copyDocumentRequests = children.map(async child => await this.copyDocument(child.id, newEntry.id)) await Promise.all(copyDocumentRequests); } //if i am in the first call ( the one with the command id given as a param //i can updated the command status ( status of the operation ) if (commandId){ this.commands[commandId].status = CommandStatus.COMPLETED; } } catch (error: unknown) { //again, if i am in the first call and i get an error i update the status and error param if (commandId){ this.commands[commandId].status = CommandStatus.FAILED; this.commands[commandId].error = error; } else { //if i am in a recursive call ( copying a child file ) , and i fail //i throw the error and hopefully it will be processed in the first call of the function throw(error) } } };

¿Funciona esta implementación? ¿Se podría mejorar?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda