Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
Treating errors in recursive function

I have a function that copies collections of files, so, i made it recursive, first i call it with the name of the collection to say so, after that, i call the function with each child file of that collection.

My issue is, i have to updated the status of the operation at the end ( succes or fail ), and this being a recursive function it is a bit hard to understand if i'm doing it right.

My solution at this moment looks like this:

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

Does this implementation work? Could it be improved?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda