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

193
Vistas
Nodejs Execute after a recursive function is done

I have a controller that basically performs a recursive call to retrieve how categories and their deleted items, but a "category_item" has a property called "next_steps" which is an array of categories, so I need to retrieve those categories and their items as well.

So I made a recursive function, it works, but I need to run something else when it actually ends, how can I do that? here's my code:

const recoveryCategory = async (req, res) => {
    try {
        const categoryId = req.params.categoryId
        const category = await nmCategorySvcV2.getByIdDeleted(categoryId)

        if (category === null){
            throw new Error("This category is not deleted.")
        }

        __recoveryRecursive(categoryId)
        // run something after 


        res.json({ success: true, message: "Success." })
    } catch (err){
        res.json({ success: false, message: err.message })
    }
}

const __recoveryRecursive = async (categoryId) => {
    const category = await nmCategorySvcV2.getByIdDeleted(categoryId)
    if (category === null){
        return
    }

    await nmCategorySvcV2.update(categoryId, { deleted: false })

    const categoryItens = await categoryItemSvcV2.getAllItensByCategory(categoryId)

    for (let i = 0; i < categoryItens.length; i++) {
        const categoryItem = categoryItens[i]

        if (categoryItem.deleted == true) {
            const item = await itemSvcV2.getByIdNoPopulate(categoryItem.item, categoryItem.page)

            if (item.deleted == true) {
                itemSvcV2.update(item._id, { deleted: false })
                categoryItemSvcV2.updateItensProp(item._id, { deleted: false })
            }

            const itemPrice = await itemPriceSvcV2.getById(categoryItem.price)

            if (itemPrice.deleted == true) {
                itemPriceSvcV2.updateObject({ _id: itemPrice._id }, { deleted: false })
            }

            categoryItemSvcV2.update(categoryItem._id, { deleted: false })

            if (categoryItem.next_steps.length > 0){
                for (let j = 0; j < categoryItem.next_steps.length; j++){
                    const categryNextStep = categoryItem.next_steps[j].category
                    __recoveryRecursive(categryNextStep)
                }
            }
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just add await before the recursive call because it returns a promise wich must be handled .

about 4 years ago · Juan Pablo Isaza Denunciar
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